Skip to content

Commit ac052da

Browse files
committed
Merge from 'master' to 'sycl-web' (#429)
CONFLICT (content): Merge conflict in clang/include/clang/Driver/Options.td
2 parents 3328bce + a4451d8 commit ac052da

File tree

1,649 files changed

+50597
-25797
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

1,649 files changed

+50597
-25797
lines changed

clang-tools-extra/clang-tidy/bugprone/BranchCloneCheck.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,8 @@ namespace bugprone {
5959

6060
void BranchCloneCheck::registerMatchers(MatchFinder *Finder) {
6161
Finder->addMatcher(
62-
ifStmt(stmt().bind("if"),
62+
ifStmt(unless(allOf(isConstexpr(), isInTemplateInstantiation())),
63+
stmt().bind("if"),
6364
hasParent(stmt(unless(ifStmt(hasElse(equalsBoundNode("if")))))),
6465
hasElse(stmt().bind("else"))),
6566
this);

clang-tools-extra/clang-tidy/bugprone/BugproneTidyModule.cpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@
3636
#include "NotNullTerminatedResultCheck.h"
3737
#include "ParentVirtualCallCheck.h"
3838
#include "PosixReturnCheck.h"
39+
#include "ReservedIdentifierCheck.h"
3940
#include "SignedCharMisuseCheck.h"
4041
#include "SizeofContainerCheck.h"
4142
#include "SizeofExpressionCheck.h"
@@ -120,6 +121,8 @@ class BugproneModule : public ClangTidyModule {
120121
"bugprone-parent-virtual-call");
121122
CheckFactories.registerCheck<PosixReturnCheck>(
122123
"bugprone-posix-return");
124+
CheckFactories.registerCheck<ReservedIdentifierCheck>(
125+
"bugprone-reserved-identifier");
123126
CheckFactories.registerCheck<SignedCharMisuseCheck>(
124127
"bugprone-signed-char-misuse");
125128
CheckFactories.registerCheck<SizeofContainerCheck>(

clang-tools-extra/clang-tidy/bugprone/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ add_clang_library(clangTidyBugproneModule
2828
NotNullTerminatedResultCheck.cpp
2929
ParentVirtualCallCheck.cpp
3030
PosixReturnCheck.cpp
31+
ReservedIdentifierCheck.cpp
3132
SignedCharMisuseCheck.cpp
3233
SizeofContainerCheck.cpp
3334
SizeofExpressionCheck.cpp
Lines changed: 179 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,179 @@
1+
//===--- ReservedIdentifierCheck.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 "ReservedIdentifierCheck.h"
10+
#include "../utils/Matchers.h"
11+
#include "../utils/OptionsUtils.h"
12+
#include "clang/AST/ASTContext.h"
13+
#include "clang/ASTMatchers/ASTMatchFinder.h"
14+
#include <algorithm>
15+
#include <cctype>
16+
17+
using namespace clang::ast_matchers;
18+
19+
namespace clang {
20+
namespace tidy {
21+
namespace bugprone {
22+
23+
static const char DoubleUnderscoreTag[] = "du";
24+
static const char UnderscoreCapitalTag[] = "uc";
25+
static const char GlobalUnderscoreTag[] = "global-under";
26+
static const char NonReservedTag[] = "non-reserved";
27+
28+
static const char Message[] =
29+
"declaration uses identifier '%0', which is %select{a reserved "
30+
"identifier|not a reserved identifier|reserved in the global namespace}1";
31+
32+
static int getMessageSelectIndex(StringRef Tag) {
33+
if (Tag == NonReservedTag)
34+
return 1;
35+
if (Tag == GlobalUnderscoreTag)
36+
return 2;
37+
return 0;
38+
}
39+
40+
ReservedIdentifierCheck::ReservedIdentifierCheck(StringRef Name,
41+
ClangTidyContext *Context)
42+
: RenamerClangTidyCheck(Name, Context),
43+
Invert(Options.get("Invert", false)),
44+
AllowedIdentifiers(utils::options::parseStringList(
45+
Options.get("AllowedIdentifiers", ""))) {}
46+
47+
void ReservedIdentifierCheck::storeOptions(ClangTidyOptions::OptionMap &Opts) {
48+
Options.store(Opts, "Invert", Invert);
49+
Options.store(Opts, "AllowedIdentifiers",
50+
utils::options::serializeStringList(AllowedIdentifiers));
51+
}
52+
53+
static std::string collapseConsecutive(StringRef Str, char C) {
54+
std::string Result;
55+
std::unique_copy(Str.begin(), Str.end(), std::back_inserter(Result),
56+
[C](char A, char B) { return A == C && B == C; });
57+
return Result;
58+
}
59+
60+
static bool hasReservedDoubleUnderscore(StringRef Name,
61+
const LangOptions &LangOpts) {
62+
if (LangOpts.CPlusPlus)
63+
return Name.find("__") != StringRef::npos;
64+
return Name.startswith("__");
65+
}
66+
67+
static Optional<std::string>
68+
getDoubleUnderscoreFixup(StringRef Name, const LangOptions &LangOpts) {
69+
if (hasReservedDoubleUnderscore(Name, LangOpts))
70+
return collapseConsecutive(Name, '_');
71+
return None;
72+
}
73+
74+
static bool startsWithUnderscoreCapital(StringRef Name) {
75+
return Name.size() >= 2 && Name[0] == '_' && std::isupper(Name[1]);
76+
}
77+
78+
static Optional<std::string> getUnderscoreCapitalFixup(StringRef Name) {
79+
if (startsWithUnderscoreCapital(Name))
80+
return std::string(Name.drop_front(1));
81+
return None;
82+
}
83+
84+
static bool startsWithUnderscoreInGlobalNamespace(StringRef Name,
85+
bool IsInGlobalNamespace) {
86+
return IsInGlobalNamespace && Name.size() >= 1 && Name[0] == '_';
87+
}
88+
89+
static Optional<std::string>
90+
getUnderscoreGlobalNamespaceFixup(StringRef Name, bool IsInGlobalNamespace) {
91+
if (startsWithUnderscoreInGlobalNamespace(Name, IsInGlobalNamespace))
92+
return std::string(Name.drop_front(1));
93+
return None;
94+
}
95+
96+
static std::string getNonReservedFixup(std::string Name) {
97+
assert(!Name.empty());
98+
if (Name[0] == '_' || std::isupper(Name[0]))
99+
Name.insert(Name.begin(), '_');
100+
else
101+
Name.insert(Name.begin(), 2, '_');
102+
return Name;
103+
}
104+
105+
static Optional<RenamerClangTidyCheck::FailureInfo>
106+
getFailureInfoImpl(StringRef Name, bool IsInGlobalNamespace,
107+
const LangOptions &LangOpts, bool Invert,
108+
ArrayRef<std::string> AllowedIdentifiers) {
109+
assert(!Name.empty());
110+
if (llvm::is_contained(AllowedIdentifiers, Name))
111+
return None;
112+
113+
// TODO: Check for names identical to language keywords, and other names
114+
// specifically reserved by language standards, e.g. C++ 'zombie names' and C
115+
// future library directions
116+
117+
using FailureInfo = RenamerClangTidyCheck::FailureInfo;
118+
if (!Invert) {
119+
Optional<FailureInfo> Info;
120+
auto AppendFailure = [&](StringRef Kind, std::string &&Fixup) {
121+
if (!Info) {
122+
Info = FailureInfo{Kind, std::move(Fixup)};
123+
} else {
124+
Info->KindName += Kind;
125+
Info->Fixup = std::move(Fixup);
126+
}
127+
};
128+
auto InProgressFixup = [&] {
129+
return Info
130+
.map([](const FailureInfo &Info) { return StringRef(Info.Fixup); })
131+
.getValueOr(Name);
132+
};
133+
if (auto Fixup = getDoubleUnderscoreFixup(InProgressFixup(), LangOpts))
134+
AppendFailure(DoubleUnderscoreTag, std::move(*Fixup));
135+
if (auto Fixup = getUnderscoreCapitalFixup(InProgressFixup()))
136+
AppendFailure(UnderscoreCapitalTag, std::move(*Fixup));
137+
if (auto Fixup = getUnderscoreGlobalNamespaceFixup(InProgressFixup(),
138+
IsInGlobalNamespace))
139+
AppendFailure(GlobalUnderscoreTag, std::move(*Fixup));
140+
141+
return Info;
142+
}
143+
if (!(hasReservedDoubleUnderscore(Name, LangOpts) ||
144+
startsWithUnderscoreCapital(Name) ||
145+
startsWithUnderscoreInGlobalNamespace(Name, IsInGlobalNamespace)))
146+
return FailureInfo{NonReservedTag, getNonReservedFixup(Name)};
147+
return None;
148+
}
149+
150+
Optional<RenamerClangTidyCheck::FailureInfo>
151+
ReservedIdentifierCheck::GetDeclFailureInfo(const NamedDecl *Decl,
152+
const SourceManager &) const {
153+
assert(Decl && Decl->getIdentifier() && !Decl->getName().empty() &&
154+
!Decl->isImplicit() &&
155+
"Decl must be an explicit identifier with a name.");
156+
return getFailureInfoImpl(Decl->getName(),
157+
isa<TranslationUnitDecl>(Decl->getDeclContext()),
158+
getLangOpts(), Invert, AllowedIdentifiers);
159+
}
160+
161+
Optional<RenamerClangTidyCheck::FailureInfo>
162+
ReservedIdentifierCheck::GetMacroFailureInfo(const Token &MacroNameTok,
163+
const SourceManager &) const {
164+
return getFailureInfoImpl(MacroNameTok.getIdentifierInfo()->getName(), true,
165+
getLangOpts(), Invert, AllowedIdentifiers);
166+
}
167+
168+
RenamerClangTidyCheck::DiagInfo
169+
ReservedIdentifierCheck::GetDiagInfo(const NamingCheckId &ID,
170+
const NamingCheckFailure &Failure) const {
171+
return DiagInfo{Message, [&](DiagnosticBuilder &diag) {
172+
diag << ID.second
173+
<< getMessageSelectIndex(Failure.Info.KindName);
174+
}};
175+
};
176+
177+
} // namespace bugprone
178+
} // namespace tidy
179+
} // namespace clang
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
//===--- ReservedIdentifierCheck.h - clang-tidy -----------------*- C++ -*-===//
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+
#ifndef LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_BUGPRONE_RESERVEDIDENTIFIERCHECK_H
10+
#define LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_BUGPRONE_RESERVEDIDENTIFIERCHECK_H
11+
12+
#include "../utils/RenamerClangTidyCheck.h"
13+
#include "llvm/ADT/Optional.h"
14+
#include <string>
15+
#include <vector>
16+
17+
namespace clang {
18+
namespace tidy {
19+
namespace bugprone {
20+
21+
/// Checks for usages of identifiers reserved for use by the implementation.
22+
///
23+
/// The C and C++ standards both reserve the following names for such use:
24+
/// * identifiers that begin with an underscore followed by an uppercase letter;
25+
/// * identifiers in the global namespace that begin with an underscore.
26+
///
27+
/// The C standard additionally reserves names beginning with a double
28+
/// underscore, while the C++ standard strengthens this to reserve names with a
29+
/// double underscore occurring anywhere.
30+
///
31+
/// For the user-facing documentation see:
32+
/// http://clang.llvm.org/extra/clang-tidy/checks/bugprone-reserved-identifier.html
33+
class ReservedIdentifierCheck final : public RenamerClangTidyCheck {
34+
const bool Invert;
35+
const std::vector<std::string> AllowedIdentifiers;
36+
37+
public:
38+
ReservedIdentifierCheck(StringRef Name, ClangTidyContext *Context);
39+
40+
void storeOptions(ClangTidyOptions::OptionMap &Opts) override;
41+
42+
private:
43+
llvm::Optional<FailureInfo>
44+
GetDeclFailureInfo(const NamedDecl *Decl,
45+
const SourceManager &SM) const override;
46+
llvm::Optional<FailureInfo>
47+
GetMacroFailureInfo(const Token &MacroNameTok,
48+
const SourceManager &SM) const override;
49+
DiagInfo GetDiagInfo(const NamingCheckId &ID,
50+
const NamingCheckFailure &Failure) const override;
51+
};
52+
53+
} // namespace bugprone
54+
} // namespace tidy
55+
} // namespace clang
56+
57+
#endif // LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_BUGPRONE_RESERVEDIDENTIFIERCHECK_H

clang-tools-extra/clang-tidy/cert/CERTTidyModule.cpp

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,9 @@
99
#include "../ClangTidy.h"
1010
#include "../ClangTidyModule.h"
1111
#include "../ClangTidyModuleRegistry.h"
12-
#include "../bugprone/UnhandledSelfAssignmentCheck.h"
1312
#include "../bugprone/BadSignalToKillThreadCheck.h"
13+
#include "../bugprone/ReservedIdentifierCheck.h"
14+
#include "../bugprone/UnhandledSelfAssignmentCheck.h"
1415
#include "../google/UnnamedNamespaceInHeaderCheck.h"
1516
#include "../misc/NewDeleteOverloadsCheck.h"
1617
#include "../misc/NonCopyableObjects.h"
@@ -44,6 +45,8 @@ class CERTModule : public ClangTidyModule {
4445
CheckFactories.registerCheck<PostfixOperatorCheck>(
4546
"cert-dcl21-cpp");
4647
CheckFactories.registerCheck<VariadicFunctionDefCheck>("cert-dcl50-cpp");
48+
CheckFactories.registerCheck<bugprone::ReservedIdentifierCheck>(
49+
"cert-dcl51-cpp");
4750
CheckFactories.registerCheck<misc::NewDeleteOverloadsCheck>(
4851
"cert-dcl54-cpp");
4952
CheckFactories.registerCheck<DontModifyStdNamespaceCheck>(
@@ -78,6 +81,8 @@ class CERTModule : public ClangTidyModule {
7881
CheckFactories.registerCheck<misc::StaticAssertCheck>("cert-dcl03-c");
7982
CheckFactories.registerCheck<readability::UppercaseLiteralSuffixCheck>(
8083
"cert-dcl16-c");
84+
CheckFactories.registerCheck<bugprone::ReservedIdentifierCheck>(
85+
"cert-dcl37-c");
8186
// ENV
8287
CheckFactories.registerCheck<CommandProcessorCheck>("cert-env33-c");
8388
// FLP

clang-tools-extra/clang-tidy/readability/BracesAroundStatementsCheck.cpp

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -123,7 +123,10 @@ void BracesAroundStatementsCheck::storeOptions(
123123
}
124124

125125
void BracesAroundStatementsCheck::registerMatchers(MatchFinder *Finder) {
126-
Finder->addMatcher(ifStmt().bind("if"), this);
126+
Finder->addMatcher(
127+
ifStmt(unless(allOf(isConstexpr(), isInTemplateInstantiation())))
128+
.bind("if"),
129+
this);
127130
Finder->addMatcher(whileStmt().bind("while"), this);
128131
Finder->addMatcher(doStmt().bind("do"), this);
129132
Finder->addMatcher(forStmt().bind("for"), this);

0 commit comments

Comments
 (0)