Skip to content

Commit 014deb6

Browse files
committed
LLVM and SPIRV-LLVM-Translator pulldown (WW49)
LLVM: llvm/llvm-project@4709bacf18b4 SPIRV-LLVM-Translator: KhronosGroup/SPIRV-LLVM-Translator@ef0015c
2 parents f4ad3c1 + 40e543f commit 014deb6

File tree

1,150 files changed

+30859
-18650
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,150 files changed

+30859
-18650
lines changed

clang-tools-extra/clangd/CodeComplete.cpp

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1272,8 +1272,7 @@ bool semaCodeComplete(std::unique_ptr<CodeCompleteConsumer> Consumer,
12721272
// Force them to be deserialized so SemaCodeComplete sees them.
12731273
loadMainFilePreambleMacros(Clang->getPreprocessor(), Input.Preamble);
12741274
if (Includes)
1275-
Clang->getPreprocessor().addPPCallbacks(
1276-
Includes->collect(Clang->getSourceManager()));
1275+
Clang->getPreprocessor().addPPCallbacks(Includes->collect(*Clang));
12771276
if (llvm::Error Err = Action.Execute()) {
12781277
log("Execute() failed when running codeComplete for {0}: {1}",
12791278
Input.FileName, toString(std::move(Err)));

clang-tools-extra/clangd/Headers.cpp

Lines changed: 17 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -22,12 +22,12 @@
2222

2323
namespace clang {
2424
namespace clangd {
25-
namespace {
2625

27-
class RecordHeaders : public PPCallbacks {
26+
class IncludeStructure::RecordHeaders : public PPCallbacks {
2827
public:
29-
RecordHeaders(const SourceManager &SM, IncludeStructure *Out)
30-
: SM(SM), Out(Out) {}
28+
RecordHeaders(const SourceManager &SM, HeaderSearch &HeaderInfo,
29+
IncludeStructure *Out)
30+
: SM(SM), HeaderInfo(HeaderInfo), Out(Out) {}
3131

3232
// Record existing #includes - both written and resolved paths. Only #includes
3333
// in the main file are collected.
@@ -85,10 +85,17 @@ class RecordHeaders : public PPCallbacks {
8585
InBuiltinFile = true;
8686
}
8787
break;
88-
case PPCallbacks::ExitFile:
88+
case PPCallbacks::ExitFile: {
8989
if (PrevFID == BuiltinFile)
9090
InBuiltinFile = false;
91+
// At file exit time HeaderSearchInfo is valid and can be used to
92+
// determine whether the file was a self-contained header or not.
93+
if (const FileEntry *FE = SM.getFileEntryForID(PrevFID))
94+
if (!isSelfContainedHeader(FE, PrevFID, SM, HeaderInfo))
95+
Out->NonSelfContained.insert(
96+
*Out->getID(SM.getFileEntryForID(PrevFID)));
9197
break;
98+
}
9299
case PPCallbacks::RenameFile:
93100
case PPCallbacks::SystemHeaderPragma:
94101
break;
@@ -97,6 +104,7 @@ class RecordHeaders : public PPCallbacks {
97104

98105
private:
99106
const SourceManager &SM;
107+
HeaderSearch &HeaderInfo;
100108
// Set after entering the <built-in> file.
101109
FileID BuiltinFile;
102110
// Indicates whether <built-in> file is part of include stack.
@@ -105,8 +113,6 @@ class RecordHeaders : public PPCallbacks {
105113
IncludeStructure *Out;
106114
};
107115

108-
} // namespace
109-
110116
bool isLiteralInclude(llvm::StringRef Include) {
111117
return Include.startswith("<") || Include.startswith("\"");
112118
}
@@ -152,9 +158,11 @@ llvm::SmallVector<llvm::StringRef, 1> getRankedIncludes(const Symbol &Sym) {
152158
}
153159

154160
std::unique_ptr<PPCallbacks>
155-
IncludeStructure::collect(const SourceManager &SM) {
161+
IncludeStructure::collect(const CompilerInstance &CI) {
162+
auto &SM = CI.getSourceManager();
156163
MainFileEntry = SM.getFileEntryForID(SM.getMainFileID());
157-
return std::make_unique<RecordHeaders>(SM, this);
164+
return std::make_unique<RecordHeaders>(
165+
SM, CI.getPreprocessor().getHeaderSearchInfo(), this);
158166
}
159167

160168
llvm::Optional<IncludeStructure::HeaderID>

clang-tools-extra/clangd/Headers.h

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,12 @@
1717
#include "clang/Basic/FileEntry.h"
1818
#include "clang/Basic/TokenKinds.h"
1919
#include "clang/Format/Format.h"
20+
#include "clang/Frontend/CompilerInstance.h"
2021
#include "clang/Lex/HeaderSearch.h"
2122
#include "clang/Lex/PPCallbacks.h"
2223
#include "clang/Tooling/Inclusions/HeaderIncludes.h"
2324
#include "llvm/ADT/ArrayRef.h"
25+
#include "llvm/ADT/DenseSet.h"
2426
#include "llvm/ADT/StringRef.h"
2527
#include "llvm/ADT/StringSet.h"
2628
#include "llvm/Support/Error.h"
@@ -123,7 +125,7 @@ class IncludeStructure {
123125

124126
// Returns a PPCallback that visits all inclusions in the main file and
125127
// populates the structure.
126-
std::unique_ptr<PPCallbacks> collect(const SourceManager &SM);
128+
std::unique_ptr<PPCallbacks> collect(const CompilerInstance &CI);
127129

128130
// HeaderID identifies file in the include graph. It corresponds to a
129131
// FileEntry rather than a FileID, but stays stable across preamble & main
@@ -138,6 +140,10 @@ class IncludeStructure {
138140
return RealPathNames[static_cast<unsigned>(ID)];
139141
}
140142

143+
bool isSelfContained(HeaderID ID) const {
144+
return !NonSelfContained.contains(ID);
145+
}
146+
141147
// Return all transitively reachable files.
142148
llvm::ArrayRef<std::string> allHeaders() const { return RealPathNames; }
143149

@@ -158,6 +164,8 @@ class IncludeStructure {
158164
// content of the main file changes.
159165
static const HeaderID MainFileID = HeaderID(0u);
160166

167+
class RecordHeaders;
168+
161169
private:
162170
// MainFileEntry will be used to check if the queried file is the main file
163171
// or not.
@@ -170,6 +178,9 @@ class IncludeStructure {
170178
// and RealPathName and UniqueID are not preserved in
171179
// the preamble.
172180
llvm::DenseMap<llvm::sys::fs::UniqueID, HeaderID> UIDToIndex;
181+
// Contains HeaderIDs of all non self-contained entries in the
182+
// IncludeStructure.
183+
llvm::DenseSet<HeaderID> NonSelfContained;
173184
};
174185

175186
// Calculates insertion edit for including a new header in a file.

clang-tools-extra/clangd/IncludeCleaner.cpp

Lines changed: 41 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88

99
#include "IncludeCleaner.h"
1010
#include "Config.h"
11+
#include "Headers.h"
1112
#include "ParsedAST.h"
1213
#include "Protocol.h"
1314
#include "SourceCode.h"
@@ -16,6 +17,7 @@
1617
#include "clang/AST/ExprCXX.h"
1718
#include "clang/AST/RecursiveASTVisitor.h"
1819
#include "clang/Basic/SourceLocation.h"
20+
#include "clang/Basic/SourceManager.h"
1921
#include "clang/Lex/HeaderSearch.h"
2022
#include "clang/Lex/Preprocessor.h"
2123
#include "clang/Tooling/Syntax/Tokens.h"
@@ -221,6 +223,31 @@ bool mayConsiderUnused(const Inclusion &Inc, ParsedAST &AST) {
221223
return true;
222224
}
223225

226+
// In case symbols are coming from non self-contained header, we need to find
227+
// its first includer that is self-contained. This is the header users can
228+
// include, so it will be responsible for bringing the symbols from given
229+
// header into the scope.
230+
FileID headerResponsible(FileID ID, const SourceManager &SM,
231+
const IncludeStructure &Includes) {
232+
// Unroll the chain of non self-contained headers until we find the one that
233+
// can be included.
234+
for (const FileEntry *FE = SM.getFileEntryForID(ID); ID != SM.getMainFileID();
235+
FE = SM.getFileEntryForID(ID)) {
236+
// If FE is nullptr, we consider it to be the responsible header.
237+
if (!FE)
238+
break;
239+
auto HID = Includes.getID(FE);
240+
assert(HID && "We're iterating over headers already existing in "
241+
"IncludeStructure");
242+
if (Includes.isSelfContained(*HID))
243+
break;
244+
// The header is not self-contained: put the responsibility for its symbols
245+
// on its includer.
246+
ID = SM.getFileID(SM.getIncludeLoc(ID));
247+
}
248+
return ID;
249+
}
250+
224251
} // namespace
225252

226253
ReferencedLocations findReferencedLocations(ParsedAST &AST) {
@@ -234,20 +261,27 @@ ReferencedLocations findReferencedLocations(ParsedAST &AST) {
234261

235262
llvm::DenseSet<FileID>
236263
findReferencedFiles(const llvm::DenseSet<SourceLocation> &Locs,
237-
const SourceManager &SM) {
264+
const IncludeStructure &Includes, const SourceManager &SM) {
238265
std::vector<SourceLocation> Sorted{Locs.begin(), Locs.end()};
239266
llvm::sort(Sorted); // Group by FileID.
240-
ReferencedFiles Result(SM);
267+
ReferencedFiles Files(SM);
241268
for (auto It = Sorted.begin(); It < Sorted.end();) {
242269
FileID FID = SM.getFileID(*It);
243-
Result.add(FID, *It);
270+
Files.add(FID, *It);
244271
// Cheaply skip over all the other locations from the same FileID.
245272
// This avoids lots of redundant Loc->File lookups for the same file.
246273
do
247274
++It;
248275
while (It != Sorted.end() && SM.isInFileID(*It, FID));
249276
}
250-
return std::move(Result.Files);
277+
// If a header is not self-contained, we consider its symbols a logical part
278+
// of the including file. Therefore, mark the parents of all used
279+
// non-self-contained FileIDs as used. Perform this on FileIDs rather than
280+
// HeaderIDs, as each inclusion of a non-self-contained file is distinct.
281+
llvm::DenseSet<FileID> Result;
282+
for (FileID ID : Files.Files)
283+
Result.insert(headerResponsible(ID, SM, Includes));
284+
return Result;
251285
}
252286

253287
std::vector<const Inclusion *>
@@ -304,25 +338,21 @@ std::vector<const Inclusion *> computeUnusedIncludes(ParsedAST &AST) {
304338
const auto &SM = AST.getSourceManager();
305339

306340
auto Refs = findReferencedLocations(AST);
307-
// FIXME(kirillbobyrev): Attribute the symbols from non self-contained
308-
// headers to their parents while the information about respective
309-
// SourceLocations and FileIDs is not lost. It is not possible to do that
310-
// later when non-guarded headers included multiple times will get same
311-
// HeaderID.
312-
auto ReferencedFileIDs = findReferencedFiles(Refs, SM);
341+
auto ReferencedFileIDs = findReferencedFiles(Refs, AST.getIncludeStructure(),
342+
AST.getSourceManager());
313343
auto ReferencedHeaders =
314344
translateToHeaderIDs(ReferencedFileIDs, AST.getIncludeStructure(), SM);
315345
return getUnused(AST, ReferencedHeaders);
316346
}
317347

318348
std::vector<Diag> issueUnusedIncludesDiagnostics(ParsedAST &AST,
319349
llvm::StringRef Code) {
320-
trace::Span Tracer("IncludeCleaner::issueUnusedIncludesDiagnostics");
321350
const Config &Cfg = Config::current();
322351
if (Cfg.Diagnostics.UnusedIncludes != Config::UnusedIncludesPolicy::Strict ||
323352
Cfg.Diagnostics.SuppressAll ||
324353
Cfg.Diagnostics.Suppress.contains("unused-includes"))
325354
return {};
355+
trace::Span Tracer("IncludeCleaner::issueUnusedIncludesDiagnostics");
326356
std::vector<Diag> Result;
327357
std::string FileName =
328358
AST.getSourceManager()

clang-tools-extra/clangd/IncludeCleaner.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@ ReferencedLocations findReferencedLocations(ParsedAST &AST);
5252
/// The output only includes things SourceManager sees as files (not macro IDs).
5353
/// This can include <built-in>, <scratch space> etc that are not true files.
5454
llvm::DenseSet<FileID> findReferencedFiles(const ReferencedLocations &Locs,
55+
const IncludeStructure &Includes,
5556
const SourceManager &SM);
5657

5758
/// Maps FileIDs to the internal IncludeStructure representation (HeaderIDs).

clang-tools-extra/clangd/ParsedAST.cpp

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -446,8 +446,7 @@ ParsedAST::build(llvm::StringRef Filename, const ParseInputs &Inputs,
446446
// Important: collectIncludeStructure is registered *after* ReplayPreamble!
447447
// Otherwise we would collect the replayed includes again...
448448
// (We can't *just* use the replayed includes, they don't have Resolved path).
449-
Clang->getPreprocessor().addPPCallbacks(
450-
Includes.collect(Clang->getSourceManager()));
449+
Clang->getPreprocessor().addPPCallbacks(Includes.collect(*Clang));
451450
// Copy over the macros in the preamble region of the main file, and combine
452451
// with non-preamble macros below.
453452
MainFileMacros Macros;

clang-tools-extra/clangd/Preamble.cpp

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
#include "clang/Basic/TokenKinds.h"
2424
#include "clang/Frontend/CompilerInvocation.h"
2525
#include "clang/Frontend/FrontendActions.h"
26+
#include "clang/Lex/HeaderSearch.h"
2627
#include "clang/Lex/Lexer.h"
2728
#include "clang/Lex/PPCallbacks.h"
2829
#include "clang/Lex/Preprocessor.h"
@@ -98,14 +99,15 @@ class CppFilePreambleCallbacks : public PreambleCallbacks {
9899
CanonIncludes.addSystemHeadersMapping(CI.getLangOpts());
99100
LangOpts = &CI.getLangOpts();
100101
SourceMgr = &CI.getSourceManager();
102+
Compiler = &CI;
101103
}
102104

103105
std::unique_ptr<PPCallbacks> createPPCallbacks() override {
104106
assert(SourceMgr && LangOpts &&
105107
"SourceMgr and LangOpts must be set at this point");
106108

107109
return std::make_unique<PPChainedCallbacks>(
108-
Includes.collect(*SourceMgr),
110+
Includes.collect(*Compiler),
109111
std::make_unique<PPChainedCallbacks>(
110112
std::make_unique<CollectMainFileMacros>(*SourceMgr, Macros),
111113
collectPragmaMarksCallback(*SourceMgr, Marks)));
@@ -140,6 +142,7 @@ class CppFilePreambleCallbacks : public PreambleCallbacks {
140142
std::unique_ptr<CommentHandler> IWYUHandler = nullptr;
141143
const clang::LangOptions *LangOpts = nullptr;
142144
const SourceManager *SourceMgr = nullptr;
145+
const CompilerInstance *Compiler = nullptr;
143146
};
144147

145148
// Represents directives other than includes, where basic textual information is
@@ -283,10 +286,9 @@ scanPreamble(llvm::StringRef Contents, const tooling::CompileCommand &Cmd) {
283286
PreprocessOnlyAction Action;
284287
if (!Action.BeginSourceFile(*Clang, Clang->getFrontendOpts().Inputs[0]))
285288
return error("failed BeginSourceFile");
286-
const auto &SM = Clang->getSourceManager();
287289
Preprocessor &PP = Clang->getPreprocessor();
288290
IncludeStructure Includes;
289-
PP.addPPCallbacks(Includes.collect(SM));
291+
PP.addPPCallbacks(Includes.collect(*Clang));
290292
ScannedPreamble SP;
291293
SP.Bounds = Bounds;
292294
PP.addPPCallbacks(

clang-tools-extra/clangd/SourceCode.cpp

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1182,5 +1182,58 @@ bool isProtoFile(SourceLocation Loc, const SourceManager &SM) {
11821182
return SM.getBufferData(FID).startswith(PROTO_HEADER_COMMENT);
11831183
}
11841184

1185+
namespace {
1186+
1187+
// Is Line an #if or #ifdef directive?
1188+
// FIXME: This makes headers with #ifdef LINUX/WINDOWS/MACOS marked as non
1189+
// self-contained and is probably not what we want.
1190+
bool isIf(llvm::StringRef Line) {
1191+
Line = Line.ltrim();
1192+
if (!Line.consume_front("#"))
1193+
return false;
1194+
Line = Line.ltrim();
1195+
return Line.startswith("if");
1196+
}
1197+
1198+
// Is Line an #error directive mentioning includes?
1199+
bool isErrorAboutInclude(llvm::StringRef Line) {
1200+
Line = Line.ltrim();
1201+
if (!Line.consume_front("#"))
1202+
return false;
1203+
Line = Line.ltrim();
1204+
if (!Line.startswith("error"))
1205+
return false;
1206+
return Line.contains_insensitive(
1207+
"includ"); // Matches "include" or "including".
1208+
}
1209+
1210+
// Heuristically headers that only want to be included via an umbrella.
1211+
bool isDontIncludeMeHeader(llvm::StringRef Content) {
1212+
llvm::StringRef Line;
1213+
// Only sniff up to 100 lines or 10KB.
1214+
Content = Content.take_front(100 * 100);
1215+
for (unsigned I = 0; I < 100 && !Content.empty(); ++I) {
1216+
std::tie(Line, Content) = Content.split('\n');
1217+
if (isIf(Line) && isErrorAboutInclude(Content.split('\n').first))
1218+
return true;
1219+
}
1220+
return false;
1221+
}
1222+
1223+
} // namespace
1224+
1225+
bool isSelfContainedHeader(const FileEntry *FE, FileID FID,
1226+
const SourceManager &SM, HeaderSearch &HeaderInfo) {
1227+
// FIXME: Should files that have been #import'd be considered
1228+
// self-contained? That's really a property of the includer,
1229+
// not of the file.
1230+
if (!HeaderInfo.isFileMultipleIncludeGuarded(FE) &&
1231+
!HeaderInfo.hasFileBeenImported(FE))
1232+
return false;
1233+
// This pattern indicates that a header can't be used without
1234+
// particular preprocessor state, usually set up by another header.
1235+
return !isDontIncludeMeHeader(SM.getBufferData(FID));
1236+
}
1237+
11851238
} // namespace clangd
11861239
} // namespace clang

clang-tools-extra/clangd/SourceCode.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
#include "clang/Basic/SourceLocation.h"
2222
#include "clang/Basic/SourceManager.h"
2323
#include "clang/Format/Format.h"
24+
#include "clang/Lex/HeaderSearch.h"
2425
#include "clang/Tooling/Core/Replacement.h"
2526
#include "clang/Tooling/Syntax/Tokens.h"
2627
#include "llvm/ADT/StringRef.h"
@@ -324,6 +325,11 @@ bool isHeaderFile(llvm::StringRef FileName,
324325
/// Returns true if the given location is in a generated protobuf file.
325326
bool isProtoFile(SourceLocation Loc, const SourceManager &SourceMgr);
326327

328+
/// This scans source code, and should not be called when using a preamble.
329+
/// Prefer to access the cache in IncludeStructure::isSelfContained if you can.
330+
bool isSelfContainedHeader(const FileEntry *FE, FileID ID,
331+
const SourceManager &SM, HeaderSearch &HeaderInfo);
332+
327333
} // namespace clangd
328334
} // namespace clang
329335
#endif

clang-tools-extra/clangd/TidyProvider.cpp

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -213,7 +213,9 @@ TidyProvider disableUnusableChecks(llvm::ArrayRef<std::string> ExtraBadChecks) {
213213
// Check can choke on invalid (intermediate) c++
214214
// code, which is often the case when clangd
215215
// tries to build an AST.
216-
"-bugprone-use-after-move");
216+
"-bugprone-use-after-move",
217+
// Alias for bugprone-use-after-moe.
218+
"-hicpp-invalid-access-moved");
217219

218220
size_t Size = BadChecks.size();
219221
for (const std::string &Str : ExtraBadChecks) {

0 commit comments

Comments
 (0)