blob: f4051c34aad9cce42b3efcef1f287a420da6f3b7 [file] [log] [blame]
Gabor Horvathe2fa5302017-02-17 08:52:51 +00001//===--- DontModifyStdNamespaceCheck.cpp - clang-tidy----------------------===//
2//
Chandler Carruth2946cd72019-01-19 08:50:56 +00003// 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
Gabor Horvathe2fa5302017-02-17 08:52:51 +00006//
7//===----------------------------------------------------------------------===//
8
9#include "DontModifyStdNamespaceCheck.h"
10#include "clang/AST/ASTContext.h"
11#include "clang/ASTMatchers/ASTMatchFinder.h"
12
13using namespace clang::ast_matchers;
14
15namespace clang {
16namespace tidy {
17namespace cert {
18
19void DontModifyStdNamespaceCheck::registerMatchers(MatchFinder *Finder) {
Gabor Horvathe2fa5302017-02-17 08:52:51 +000020 Finder->addMatcher(
21 namespaceDecl(unless(isExpansionInSystemHeader()),
Nathan James41bbb872020-07-07 14:30:52 +010022 hasAnyName("std", "posix"),
Gabor Horvathe2fa5302017-02-17 08:52:51 +000023 has(decl(unless(anyOf(
24 functionDecl(isExplicitTemplateSpecialization()),
25 cxxRecordDecl(isExplicitTemplateSpecialization()))))))
26 .bind("nmspc"),
27 this);
28}
29
30void DontModifyStdNamespaceCheck::check(
31 const MatchFinder::MatchResult &Result) {
32 const auto *N = Result.Nodes.getNodeAs<NamespaceDecl>("nmspc");
33
34 // Only consider top level namespaces.
35 if (N->getParent() != Result.Context->getTranslationUnitDecl())
36 return;
37
38 diag(N->getLocation(),
39 "modification of %0 namespace can result in undefined behavior")
40 << N;
41}
42
43} // namespace cert
44} // namespace tidy
45} // namespace clang