clang-tools 23.0.0git
StdNamespaceModificationCheck.cpp
Go to the documentation of this file.
1//===----------------------------------------------------------------------===//
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
10#include "clang/ASTMatchers/ASTMatchFinder.h"
11#include "clang/ASTMatchers/ASTMatchersInternal.h"
12
13using namespace clang;
14using namespace clang::ast_matchers;
15
16namespace {
17
18AST_POLYMORPHIC_MATCHER_P(
19 hasAnyTemplateArgumentIncludingPack,
20 AST_POLYMORPHIC_SUPPORTED_TYPES(ClassTemplateSpecializationDecl,
21 TemplateSpecializationType, FunctionDecl),
22 ast_matchers::internal::Matcher<TemplateArgument>, InnerMatcher) {
23 const ArrayRef<TemplateArgument> Args =
24 ast_matchers::internal::getTemplateSpecializationArgs(Node);
25 for (const auto &Arg : Args) {
26 if (Arg.getKind() != TemplateArgument::Pack)
27 continue;
28 const ArrayRef<TemplateArgument> PackArgs = Arg.getPackAsArray();
29 if (matchesFirstInRange(InnerMatcher, PackArgs.begin(), PackArgs.end(),
30 Finder, Builder) != PackArgs.end())
31 return true;
32 }
33 return matchesFirstInRange(InnerMatcher, Args.begin(), Args.end(), Finder,
34 Builder) != Args.end();
35}
36
37} // namespace
38
39namespace clang::tidy::bugprone {
40
42 auto HasStdParent =
43 hasDeclContext(namespaceDecl(hasAnyName("std", "posix"),
44 unless(hasParent(namespaceDecl())))
45 .bind("nmspc"));
46 auto UserDefinedDecl =
47 namedDecl(anyOf(classTemplateDecl(), tagDecl()),
48 hasAncestor(namespaceDecl(hasAnyName("std", "posix"),
49 unless(hasParent(namespaceDecl())))));
50 auto UserDefinedType = qualType(hasUnqualifiedDesugaredType(anyOf(
51 tagType(unless(hasDeclaration(UserDefinedDecl))),
52 templateSpecializationType(unless(hasDeclaration(UserDefinedDecl))))));
53 auto HasNoProgramDefinedTemplateArgument = unless(
54 hasAnyTemplateArgumentIncludingPack(refersToType(UserDefinedType)));
55 auto InsideStdClassOrClassTemplateSpecialization = hasDeclContext(
56 anyOf(cxxRecordDecl(HasStdParent),
57 classTemplateSpecializationDecl(
58 HasStdParent, HasNoProgramDefinedTemplateArgument)));
59
60 // Try to follow exactly CERT rule DCL58-CPP (this text is taken from C++
61 // standard into the CERT rule):
62 // "
63 // 1 The behavior of a C++ program is undefined if it adds declarations or
64 // definitions to namespace std or to a namespace within namespace std unless
65 // otherwise specified. A program may add a template specialization for any
66 // standard library template to namespace std only if the declaration depends
67 // on a user-defined type and the specialization meets the standard library
68 // requirements for the original template and is not explicitly prohibited. 2
69 // The behavior of a C++ program is undefined if it declares — an explicit
70 // specialization of any member function of a standard library class template,
71 // or — an explicit specialization of any member function template of a
72 // standard library class or class template, or — an explicit or partial
73 // specialization of any member class template of a standard library class or
74 // class template.
75 // "
76 // The "standard library requirements" and explicit prohibition are not
77 // checked.
78
79 auto BadNonTemplateSpecializationDecl =
80 decl(unless(anyOf(functionDecl(isExplicitTemplateSpecialization()),
81 varDecl(isExplicitTemplateSpecialization()),
82 cxxRecordDecl(isExplicitTemplateSpecialization()))),
83 HasStdParent);
84 auto BadClassTemplateSpec = classTemplateSpecializationDecl(
85 HasNoProgramDefinedTemplateArgument, HasStdParent);
86 auto BadInnerClassTemplateSpec = classTemplateSpecializationDecl(
87 InsideStdClassOrClassTemplateSpecialization);
88 auto BadFunctionTemplateSpec =
89 functionDecl(unless(cxxMethodDecl()), isExplicitTemplateSpecialization(),
90 HasNoProgramDefinedTemplateArgument, HasStdParent);
91 auto BadMemberFunctionSpec =
92 cxxMethodDecl(isExplicitTemplateSpecialization(),
93 InsideStdClassOrClassTemplateSpecialization);
94
95 Finder->addMatcher(decl(anyOf(BadNonTemplateSpecializationDecl,
96 BadClassTemplateSpec, BadInnerClassTemplateSpec,
97 BadFunctionTemplateSpec, BadMemberFunctionSpec))
98 .bind("decl"),
99 this);
100}
101} // namespace clang::tidy::bugprone
102
103static const NamespaceDecl *getTopLevelLexicalNamespaceDecl(const Decl *D) {
104 const NamespaceDecl *LastNS = nullptr;
105 while (D) {
106 if (const auto *NS = dyn_cast<NamespaceDecl>(D))
107 LastNS = NS;
108 D = dyn_cast_or_null<Decl>(D->getLexicalDeclContext());
109 }
110 return LastNS;
111}
112
114 const MatchFinder::MatchResult &Result) {
115 const auto *D = Result.Nodes.getNodeAs<Decl>("decl");
116 const auto *NS = Result.Nodes.getNodeAs<NamespaceDecl>("nmspc");
117 if (!D || !NS)
118 return;
119
120 diag(D->getLocation(),
121 "modification of %0 namespace can result in undefined behavior")
122 << NS;
123 // 'NS' is not always the namespace declaration that lexically contains 'D',
124 // try to find such a namespace.
125 if (const NamespaceDecl *LexNS = getTopLevelLexicalNamespaceDecl(D)) {
126 assert(NS->getCanonicalDecl() == LexNS->getCanonicalDecl() &&
127 "Mismatch in found namespace");
128 diag(LexNS->getLocation(), "%0 namespace opened here", DiagnosticIDs::Note)
129 << LexNS;
130 }
131}
static const NamespaceDecl * getTopLevelLexicalNamespaceDecl(const Decl *D)
void check(const ast_matchers::MatchFinder::MatchResult &Result) override
void registerMatchers(ast_matchers::MatchFinder *Finder) override
===– Representation.cpp - ClangDoc Representation --------—*- C++ -*-===//