clang-tools 6.0.0

DontModifyStdNamespaceCheck.cpp
Go to the documentation of this file.
1 //===--- DontModifyStdNamespaceCheck.cpp - clang-tidy----------------------===//
2 //
3 // The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9
10 #include "DontModifyStdNamespaceCheck.h"
11 #include "clang/AST/ASTContext.h"
12 #include "clang/ASTMatchers/ASTMatchFinder.h"
13
14 using namespace clang::ast_matchers;
15
16 namespace clang {
17 namespace tidy {
18 namespace cert {
19
20 void DontModifyStdNamespaceCheck::registerMatchers(MatchFinder *Finder) {
21 if (!getLangOpts().CPlusPlus)
22 return;
23
24 Finder->addMatcher(
25 namespaceDecl(unless(isExpansionInSystemHeader()),
26 anyOf(hasName("std"), hasName("posix")),
27 has(decl(unless(anyOf(
28 functionDecl(isExplicitTemplateSpecialization()),
29 cxxRecordDecl(isExplicitTemplateSpecialization()))))))
30 .bind("nmspc"),
31 this);
32 }
33
34 void DontModifyStdNamespaceCheck::check(
35 const MatchFinder::MatchResult &Result) {
36 const auto *N = Result.Nodes.getNodeAs<NamespaceDecl>("nmspc");
37
38 // Only consider top level namespaces.
39 if (N->getParent() != Result.Context->getTranslationUnitDecl())
40 return;
41
42 diag(N->getLocation(),
43 "modification of %0 namespace can result in undefined behavior")
44 << N;
45 }
46
47 } // namespace cert
48 } // namespace tidy
49 } // namespace clang
clang::ast_matchers
Definition: ClangTidyDiagnosticConsumer.h:27
clang
Definition: AndroidTidyModule.cpp:28
DontModifyStdNamespaceCheck.h