1//===--- UnnamedNamespaceInHeaderCheck.cpp - clang-tidy ---------*- C++ -*-===//
2//
3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6//
7//===----------------------------------------------------------------------===//
8
9#include "UnnamedNamespaceInHeaderCheck.h"
10#include "clang/ASTMatchers/ASTMatchFinder.h"
11#include "clang/ASTMatchers/ASTMatchers.h"
12
13using namespace clang::ast_matchers;
14
15namespace clang::tidy::google::build {
16
17UnnamedNamespaceInHeaderCheck::UnnamedNamespaceInHeaderCheck(
18 StringRef Name, ClangTidyContext *Context)
19 : ClangTidyCheck(Name, Context),
20 HeaderFileExtensions(Context->getHeaderFileExtensions()) {}
21
22void UnnamedNamespaceInHeaderCheck::registerMatchers(
23 ast_matchers::MatchFinder *Finder) {
24 Finder->addMatcher(NodeMatch: namespaceDecl(isAnonymous()).bind(ID: "anonymousNamespace"),
25 Action: this);
26}
27
28void UnnamedNamespaceInHeaderCheck::check(
29 const MatchFinder::MatchResult &Result) {
30 const auto *N = Result.Nodes.getNodeAs<NamespaceDecl>(ID: "anonymousNamespace");
31 SourceLocation Loc = N->getBeginLoc();
32 if (!Loc.isValid())
33 return;
34
35 if (utils::isPresumedLocInHeaderFile(Loc, SM&: *Result.SourceManager,
36 HeaderFileExtensions))
37 diag(Loc, Description: "do not use unnamed namespaces in header files");
38}
39
40} // namespace clang::tidy::google::build
41

Provided by KDAB

Privacy Policy
Update your C++ knowledge – Modern C++11/14/17 Training
Find out more

source code of clang-tools-extra/clang-tidy/google/UnnamedNamespaceInHeaderCheck.cpp