Skip to content

Commit 060a5f6

Browse files
authored
LLVM and SPIRV-LLVM-Translator pulldown (WW46)
LLVM: llvm/llvm-project@23657d9 SPIRV-LLVM-Translator: KhronosGroup/SPIRV-LLVM-Translator@a825cdb
2 parents 7b7775e + 63f3e6d commit 060a5f6

File tree

3,389 files changed

+242795
-166461
lines changed

Some content is hidden

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

3,389 files changed

+242795
-166461
lines changed

clang-tools-extra/clang-tidy/ClangTidy.cpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,9 @@ class ErrorReporter {
110110
DiagOpts->ShowColors = Context.getOptions().UseColor.getValueOr(
111111
llvm::sys::Process::StandardOutHasColors());
112112
DiagPrinter->BeginSourceFile(LangOpts);
113+
if (DiagOpts->ShowColors && !llvm::sys::Process::StandardOutIsDisplayed()) {
114+
llvm::sys::Process::UseANSIEscapeCodes(true);
115+
}
113116
}
114117

115118
SourceManager &getSourceManager() { return SourceMgr; }

clang-tools-extra/clang-tidy/ExpandModularHeadersPPCallbacks.cpp

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -52,9 +52,11 @@ class ExpandModularHeadersPPCallbacks::FileRecorder {
5252
/// Makes sure we have contents for all the files we were interested in. Ideally
5353
/// `FilesToRecord` should be empty.
5454
void checkAllFilesRecorded() {
55-
for (auto FileEntry : FilesToRecord)
56-
LLVM_DEBUG(llvm::dbgs() << "Did not record contents for input file: "
57-
<< FileEntry->getName() << "\n");
55+
LLVM_DEBUG({
56+
for (auto FileEntry : FilesToRecord)
57+
llvm::dbgs() << "Did not record contents for input file: "
58+
<< FileEntry->getName() << "\n";
59+
});
5860
}
5961

6062
private:

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@
4040
#include "PosixReturnCheck.h"
4141
#include "RedundantBranchConditionCheck.h"
4242
#include "ReservedIdentifierCheck.h"
43+
#include "SignalHandlerCheck.h"
4344
#include "SignedCharMisuseCheck.h"
4445
#include "SizeofContainerCheck.h"
4546
#include "SizeofExpressionCheck.h"
@@ -133,6 +134,7 @@ class BugproneModule : public ClangTidyModule {
133134
"bugprone-posix-return");
134135
CheckFactories.registerCheck<ReservedIdentifierCheck>(
135136
"bugprone-reserved-identifier");
137+
CheckFactories.registerCheck<SignalHandlerCheck>("bugprone-signal-handler");
136138
CheckFactories.registerCheck<SignedCharMisuseCheck>(
137139
"bugprone-signed-char-misuse");
138140
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
@@ -35,6 +35,7 @@ add_clang_library(clangTidyBugproneModule
3535
PosixReturnCheck.cpp
3636
RedundantBranchConditionCheck.cpp
3737
ReservedIdentifierCheck.cpp
38+
SignalHandlerCheck.cpp
3839
SignedCharMisuseCheck.cpp
3940
SizeofContainerCheck.cpp
4041
SizeofExpressionCheck.cpp
Lines changed: 186 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,186 @@
1+
//===--- SignalHandlerCheck.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 "SignalHandlerCheck.h"
10+
#include "clang/AST/ASTContext.h"
11+
#include "clang/AST/RecursiveASTVisitor.h"
12+
#include "clang/ASTMatchers/ASTMatchFinder.h"
13+
#include "clang/Analysis/CallGraph.h"
14+
#include "llvm/ADT/DenseSet.h"
15+
#include "llvm/ADT/STLExtras.h"
16+
#include "llvm/ADT/SmallVector.h"
17+
#include <iterator>
18+
#include <queue>
19+
20+
using namespace clang::ast_matchers;
21+
22+
namespace clang {
23+
namespace tidy {
24+
namespace bugprone {
25+
26+
static bool isSystemCall(const FunctionDecl *FD) {
27+
// Find a possible redeclaration in system header.
28+
// FIXME: Looking at the canonical declaration is not the most exact way
29+
// to do this.
30+
31+
// Most common case will be inclusion directly from a header.
32+
// This works fine by using canonical declaration.
33+
// a.c
34+
// #include <sysheader.h>
35+
36+
// Next most common case will be extern declaration.
37+
// Can't catch this with either approach.
38+
// b.c
39+
// extern void sysfunc(void);
40+
41+
// Canonical declaration is the first found declaration, so this works.
42+
// c.c
43+
// #include <sysheader.h>
44+
// extern void sysfunc(void); // redecl won't matter
45+
46+
// This does not work with canonical declaration.
47+
// Probably this is not a frequently used case but may happen (the first
48+
// declaration can be in a non-system header for example).
49+
// d.c
50+
// extern void sysfunc(void); // Canonical declaration, not in system header.
51+
// #include <sysheader.h>
52+
53+
return FD->getASTContext().getSourceManager().isInSystemHeader(
54+
FD->getCanonicalDecl()->getLocation());
55+
}
56+
57+
AST_MATCHER(FunctionDecl, isSystemCall) { return isSystemCall(&Node); }
58+
59+
// This is the minimal set of safe functions.
60+
// FIXME: Add checker option to allow a POSIX compliant extended set.
61+
llvm::StringSet<> SignalHandlerCheck::StrictConformingFunctions{
62+
"signal", "abort", "_Exit", "quick_exit"};
63+
64+
SignalHandlerCheck::SignalHandlerCheck(StringRef Name,
65+
ClangTidyContext *Context)
66+
: ClangTidyCheck(Name, Context) {}
67+
68+
bool SignalHandlerCheck::isLanguageVersionSupported(
69+
const LangOptions &LangOpts) const {
70+
// FIXME: Make the checker useful on C++ code.
71+
if (LangOpts.CPlusPlus)
72+
return false;
73+
74+
return true;
75+
}
76+
77+
void SignalHandlerCheck::registerMatchers(MatchFinder *Finder) {
78+
auto SignalFunction = functionDecl(hasAnyName("::signal", "::std::signal"),
79+
parameterCountIs(2), isSystemCall());
80+
auto HandlerExpr =
81+
declRefExpr(hasDeclaration(functionDecl().bind("handler_decl")),
82+
unless(isExpandedFromMacro("SIG_IGN")),
83+
unless(isExpandedFromMacro("SIG_DFL")))
84+
.bind("handler_expr");
85+
Finder->addMatcher(
86+
callExpr(callee(SignalFunction), hasArgument(1, HandlerExpr))
87+
.bind("register_call"),
88+
this);
89+
}
90+
91+
void SignalHandlerCheck::check(const MatchFinder::MatchResult &Result) {
92+
const auto *SignalCall = Result.Nodes.getNodeAs<CallExpr>("register_call");
93+
const auto *HandlerDecl =
94+
Result.Nodes.getNodeAs<FunctionDecl>("handler_decl");
95+
const auto *HandlerExpr = Result.Nodes.getNodeAs<DeclRefExpr>("handler_expr");
96+
97+
// Visit each function encountered in the callgraph only once.
98+
llvm::DenseSet<const FunctionDecl *> SeenFunctions;
99+
100+
// The worklist of the callgraph visitation algorithm.
101+
std::deque<const CallExpr *> CalledFunctions;
102+
103+
auto ProcessFunction = [&](const FunctionDecl *F, const Expr *CallOrRef) {
104+
// Ensure that canonical declaration is used.
105+
F = F->getCanonicalDecl();
106+
107+
// Do not visit function if already encountered.
108+
if (!SeenFunctions.insert(F).second)
109+
return true;
110+
111+
// Check if the call is allowed.
112+
// Non-system calls are not considered.
113+
if (isSystemCall(F)) {
114+
if (isSystemCallAllowed(F))
115+
return true;
116+
117+
reportBug(F, CallOrRef, SignalCall, HandlerDecl);
118+
119+
return false;
120+
}
121+
122+
// Get the body of the encountered non-system call function.
123+
const FunctionDecl *FBody;
124+
if (!F->hasBody(FBody)) {
125+
reportBug(F, CallOrRef, SignalCall, HandlerDecl);
126+
return false;
127+
}
128+
129+
// Collect all called functions.
130+
auto Matches = match(decl(forEachDescendant(callExpr().bind("call"))),
131+
*FBody, FBody->getASTContext());
132+
for (const auto &Match : Matches) {
133+
const auto *CE = Match.getNodeAs<CallExpr>("call");
134+
if (isa<FunctionDecl>(CE->getCalleeDecl()))
135+
CalledFunctions.push_back(CE);
136+
}
137+
138+
return true;
139+
};
140+
141+
if (!ProcessFunction(HandlerDecl, HandlerExpr))
142+
return;
143+
144+
// Visit the definition of every function referenced by the handler function.
145+
// Check for allowed function calls.
146+
while (!CalledFunctions.empty()) {
147+
const CallExpr *FunctionCall = CalledFunctions.front();
148+
CalledFunctions.pop_front();
149+
// At insertion we have already ensured that only function calls are there.
150+
const auto *F = cast<FunctionDecl>(FunctionCall->getCalleeDecl());
151+
152+
if (!ProcessFunction(F, FunctionCall))
153+
break;
154+
}
155+
}
156+
157+
bool SignalHandlerCheck::isSystemCallAllowed(const FunctionDecl *FD) const {
158+
const IdentifierInfo *II = FD->getIdentifier();
159+
// Unnamed functions are not explicitly allowed.
160+
if (!II)
161+
return false;
162+
163+
// FIXME: Improve for C++ (check for namespace).
164+
if (StrictConformingFunctions.count(II->getName()))
165+
return true;
166+
167+
return false;
168+
}
169+
170+
void SignalHandlerCheck::reportBug(const FunctionDecl *CalledFunction,
171+
const Expr *CallOrRef,
172+
const CallExpr *SignalCall,
173+
const FunctionDecl *HandlerDecl) {
174+
diag(CallOrRef->getBeginLoc(),
175+
"%0 may not be asynchronous-safe; "
176+
"calling it from a signal handler may be dangerous")
177+
<< CalledFunction;
178+
diag(SignalCall->getSourceRange().getBegin(),
179+
"signal handler registered here", DiagnosticIDs::Note);
180+
diag(HandlerDecl->getBeginLoc(), "handler function declared here",
181+
DiagnosticIDs::Note);
182+
}
183+
184+
} // namespace bugprone
185+
} // namespace tidy
186+
} // namespace clang
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
//===--- SignalHandlerCheck.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_SIGNALHANDLERCHECK_H
10+
#define LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_BUGPRONE_SIGNALHANDLERCHECK_H
11+
12+
#include "../ClangTidyCheck.h"
13+
#include "llvm/ADT/StringSet.h"
14+
15+
namespace clang {
16+
namespace tidy {
17+
namespace bugprone {
18+
19+
/// Checker for signal handler functions.
20+
///
21+
/// For the user-facing documentation see:
22+
/// http://clang.llvm.org/extra/clang-tidy/checks/bugprone-signal-handler-check.html
23+
class SignalHandlerCheck : public ClangTidyCheck {
24+
public:
25+
SignalHandlerCheck(StringRef Name, ClangTidyContext *Context);
26+
bool isLanguageVersionSupported(const LangOptions &LangOpts) const override;
27+
void registerMatchers(ast_matchers::MatchFinder *Finder) override;
28+
void check(const ast_matchers::MatchFinder::MatchResult &Result) override;
29+
30+
private:
31+
void reportBug(const FunctionDecl *CalledFunction, const Expr *CallOrRef,
32+
const CallExpr *SignalCall, const FunctionDecl *HandlerDecl);
33+
bool isSystemCallAllowed(const FunctionDecl *FD) const;
34+
35+
static llvm::StringSet<> StrictConformingFunctions;
36+
};
37+
38+
} // namespace bugprone
39+
} // namespace tidy
40+
} // namespace clang
41+
42+
#endif // LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_BUGPRONE_SIGNALHANDLERCHECK_H

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
#include "../ClangTidyModuleRegistry.h"
1212
#include "../bugprone/BadSignalToKillThreadCheck.h"
1313
#include "../bugprone/ReservedIdentifierCheck.h"
14+
#include "../bugprone/SignalHandlerCheck.h"
1415
#include "../bugprone/SignedCharMisuseCheck.h"
1516
#include "../bugprone/SpuriouslyWakeUpFunctionsCheck.h"
1617
#include "../bugprone/UnhandledSelfAssignmentCheck.h"
@@ -109,6 +110,8 @@ class CERTModule : public ClangTidyModule {
109110
// POS
110111
CheckFactories.registerCheck<bugprone::BadSignalToKillThreadCheck>(
111112
"cert-pos44-c");
113+
// SIG
114+
CheckFactories.registerCheck<bugprone::SignalHandlerCheck>("cert-sig30-c");
112115
// STR
113116
CheckFactories.registerCheck<bugprone::SignedCharMisuseCheck>(
114117
"cert-str34-c");

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

Lines changed: 33 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -124,19 +124,26 @@ static StringRef const StyleNames[] = {
124124

125125
static std::vector<llvm::Optional<IdentifierNamingCheck::NamingStyle>>
126126
getNamingStyles(const ClangTidyCheck::OptionsView &Options) {
127-
std::vector<llvm::Optional<IdentifierNamingCheck::NamingStyle>> Styles;
128-
Styles.reserve(array_lengthof(StyleNames));
129-
for (auto const &StyleName : StyleNames) {
130-
auto CaseOptional = Options.getOptional<IdentifierNamingCheck::CaseType>(
131-
(StyleName + "Case").str());
132-
auto Prefix = Options.get((StyleName + "Prefix").str(), "");
133-
auto Postfix = Options.get((StyleName + "Suffix").str(), "");
127+
std::vector<llvm::Optional<IdentifierNamingCheck::NamingStyle>> Styles(
128+
SK_Count);
129+
SmallString<64> StyleString;
130+
for (unsigned I = 0; I < SK_Count; ++I) {
131+
StyleString = StyleNames[I];
132+
size_t StyleSize = StyleString.size();
133+
StyleString.append("Prefix");
134+
std::string Prefix(Options.get(StyleString, ""));
135+
// Fast replacement of [Pre]fix -> [Suf]fix.
136+
memcpy(&StyleString[StyleSize], "Suf", 3);
137+
std::string Postfix(Options.get(StyleString, ""));
138+
memcpy(&StyleString[StyleSize], "Case", 4);
139+
StyleString.pop_back();
140+
StyleString.pop_back();
141+
auto CaseOptional =
142+
Options.getOptional<IdentifierNamingCheck::CaseType>(StyleString);
134143

135144
if (CaseOptional || !Prefix.empty() || !Postfix.empty())
136-
Styles.emplace_back(IdentifierNamingCheck::NamingStyle{
137-
std::move(CaseOptional), std::move(Prefix), std::move(Postfix)});
138-
else
139-
Styles.emplace_back(llvm::None);
145+
Styles[I].emplace(std::move(CaseOptional), std::move(Prefix),
146+
std::move(Postfix));
140147
}
141148
return Styles;
142149
}
@@ -161,18 +168,23 @@ IdentifierNamingCheck::~IdentifierNamingCheck() = default;
161168

162169
void IdentifierNamingCheck::storeOptions(ClangTidyOptions::OptionMap &Opts) {
163170
RenamerClangTidyCheck::storeOptions(Opts);
164-
ArrayRef<llvm::Optional<NamingStyle>> NamingStyles =
165-
getStyleForFile(Context->getCurrentFile());
171+
SmallString<64> StyleString;
166172
for (size_t I = 0; I < SK_Count; ++I) {
167-
if (!NamingStyles[I])
173+
if (!MainFileStyle[I])
168174
continue;
169-
if (NamingStyles[I]->Case)
170-
Options.store(Opts, (StyleNames[I] + "Case").str(),
171-
*NamingStyles[I]->Case);
172-
Options.store(Opts, (StyleNames[I] + "Prefix").str(),
173-
NamingStyles[I]->Prefix);
174-
Options.store(Opts, (StyleNames[I] + "Suffix").str(),
175-
NamingStyles[I]->Suffix);
175+
StyleString = StyleNames[I];
176+
size_t StyleSize = StyleString.size();
177+
StyleString.append("Prefix");
178+
Options.store(Opts, StyleString, MainFileStyle[I]->Prefix);
179+
// Fast replacement of [Pre]fix -> [Suf]fix.
180+
memcpy(&StyleString[StyleSize], "Suf", 3);
181+
Options.store(Opts, StyleString, MainFileStyle[I]->Suffix);
182+
if (MainFileStyle[I]->Case) {
183+
memcpy(&StyleString[StyleSize], "Case", 4);
184+
StyleString.pop_back();
185+
StyleString.pop_back();
186+
Options.store(Opts, StyleString, *MainFileStyle[I]->Case);
187+
}
176188
}
177189
Options.store(Opts, "GetConfigPerFile", GetConfigPerFile);
178190
Options.store(Opts, "IgnoreFailedSplit", IgnoreFailedSplit);

0 commit comments

Comments
 (0)