blob: e5759a5215a680f15c51272aba0825a388edf786 [file] [log] [blame]
Gabor Horvathe2fa5302017-02-17 08:52:51 +00001//===--- 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
14using namespace clang::ast_matchers;
15
16namespace clang {
17namespace tidy {
18namespace cert {
19
20void 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
34void 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