1 //===--- DontModifyStdNamespaceCheck.cpp - clang-tidy----------------------===// 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 "DontModifyStdNamespaceCheck.h" 10 #include "clang/AST/ASTContext.h" 11 #include "clang/ASTMatchers/ASTMatchFinder.h" 12 13 using namespace clang::ast_matchers; 14 15 namespace clang { 16 namespace tidy { 17 namespace cert { 18 19 void DontModifyStdNamespaceCheck::registerMatchers(MatchFinder *Finder) { 20 if (!getLangOpts().CPlusPlus) 21 return; 22 23 Finder->addMatcher( 24 namespaceDecl(unless(isExpansionInSystemHeader()), 25 anyOf(hasName("std"), hasName("posix")), 26 has(decl(unless(anyOf( 27 functionDecl(isExplicitTemplateSpecialization()), 28 cxxRecordDecl(isExplicitTemplateSpecialization())))))) 29 .bind("nmspc"), 30 this); 31 } 32 33 void DontModifyStdNamespaceCheck::check( 34 const MatchFinder::MatchResult &Result) { 35 const auto *N = Result.Nodes.getNodeAs<NamespaceDecl>("nmspc"); 36 37 // Only consider top level namespaces. 38 if (N->getParent() != Result.Context->getTranslationUnitDecl()) 39 return; 40 41 diag(N->getLocation(), 42 "modification of %0 namespace can result in undefined behavior") 43 << N; 44 } 45 46 } // namespace cert 47 } // namespace tidy 48 } // namespace clang 49