Skip to content

Commit 34a460d

Browse files
authored
LLVM and SPIRV-LLVM-Translator pulldown (WW17) #3573
LLVM: llvm/llvm-project@0a92aff SPIRV-LLVM-Translator: KhronosGroup/SPIRV-LLVM-Translator@b9e4fa4
2 parents ba3d657 + b9fea96 commit 34a460d

File tree

845 files changed

+160776
-7565
lines changed

Some content is hidden

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

845 files changed

+160776
-7565
lines changed

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

Lines changed: 22 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -132,6 +132,8 @@ class ErrorReporter {
132132
}
133133
auto Diag = Diags.Report(Loc, Diags.getCustomDiagID(Level, "%0 [%1]"))
134134
<< Message.Message << Name;
135+
for (const FileByteRange &FBR : Error.Message.Ranges)
136+
Diag << getRange(FBR);
135137
// FIXME: explore options to support interactive fix selection.
136138
const llvm::StringMap<Replacements> *ChosenFix;
137139
if (ApplyFixes != FB_NoFix &&
@@ -257,17 +259,13 @@ class ErrorReporter {
257259
for (const auto &Repl : FileAndReplacements.second) {
258260
if (!Repl.isApplicable())
259261
continue;
260-
SmallString<128> FixAbsoluteFilePath = Repl.getFilePath();
261-
Files.makeAbsolutePath(FixAbsoluteFilePath);
262-
SourceLocation FixLoc =
263-
getLocation(FixAbsoluteFilePath, Repl.getOffset());
264-
SourceLocation FixEndLoc = FixLoc.getLocWithOffset(Repl.getLength());
265-
// Retrieve the source range for applicable fixes. Macro definitions
266-
// on the command line have locations in a virtual buffer and don't
267-
// have valid file paths and are therefore not applicable.
268-
CharSourceRange Range =
269-
CharSourceRange::getCharRange(SourceRange(FixLoc, FixEndLoc));
270-
Diag << FixItHint::CreateReplacement(Range, Repl.getReplacementText());
262+
FileByteRange FBR;
263+
FBR.FilePath = Repl.getFilePath().str();
264+
FBR.FileOffset = Repl.getOffset();
265+
FBR.Length = Repl.getLength();
266+
267+
Diag << FixItHint::CreateReplacement(getRange(FBR),
268+
Repl.getReplacementText());
271269
}
272270
}
273271
}
@@ -277,9 +275,22 @@ class ErrorReporter {
277275
auto Diag =
278276
Diags.Report(Loc, Diags.getCustomDiagID(DiagnosticsEngine::Note, "%0"))
279277
<< Message.Message;
278+
for (const FileByteRange &FBR : Message.Ranges)
279+
Diag << getRange(FBR);
280280
reportFix(Diag, Message.Fix);
281281
}
282282

283+
CharSourceRange getRange(const FileByteRange &Range) {
284+
SmallString<128> AbsoluteFilePath{Range.FilePath};
285+
Files.makeAbsolutePath(AbsoluteFilePath);
286+
SourceLocation BeginLoc = getLocation(AbsoluteFilePath, Range.FileOffset);
287+
SourceLocation EndLoc = BeginLoc.getLocWithOffset(Range.Length);
288+
// Retrieve the source range for applicable highlights and fixes. Macro
289+
// definition on the command line have locations in a virtual buffer and
290+
// don't have valid file paths and are therefore not applicable.
291+
return CharSourceRange::getCharRange(BeginLoc, EndLoc);
292+
}
293+
283294
FileManager Files;
284295
LangOptions LangOpts; // FIXME: use langopts from each original file
285296
IntrusiveRefCntPtr<DiagnosticOptions> DiagOpts;

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

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
#include "clang/Basic/DiagnosticOptions.h"
2626
#include "clang/Basic/SourceManager.h"
2727
#include "clang/Frontend/DiagnosticRenderer.h"
28+
#include "clang/Lex/Lexer.h"
2829
#include "clang/Tooling/Core/Diagnostic.h"
2930
#include "clang/Tooling/Core/Replacement.h"
3031
#include "llvm/ADT/STLExtras.h"
@@ -62,15 +63,32 @@ class ClangTidyDiagnosticRenderer : public DiagnosticRenderer {
6263
Loc.isValid()
6364
? tooling::DiagnosticMessage(Message, Loc.getManager(), Loc)
6465
: tooling::DiagnosticMessage(Message);
66+
67+
// Make sure that if a TokenRange is receieved from the check it is unfurled
68+
// into a real CharRange for the diagnostic printer later.
69+
// Whatever we store here gets decoupled from the current SourceManager, so
70+
// we **have to** know the exact position and length of the highlight.
71+
auto ToCharRange = [this, &Loc](const CharSourceRange &SourceRange) {
72+
if (SourceRange.isCharRange())
73+
return SourceRange;
74+
assert(SourceRange.isTokenRange());
75+
SourceLocation End = Lexer::getLocForEndOfToken(
76+
SourceRange.getEnd(), 0, Loc.getManager(), LangOpts);
77+
return CharSourceRange::getCharRange(SourceRange.getBegin(), End);
78+
};
79+
6580
if (Level == DiagnosticsEngine::Note) {
6681
Error.Notes.push_back(TidyMessage);
82+
for (const CharSourceRange &SourceRange : Ranges)
83+
Error.Notes.back().Ranges.emplace_back(Loc.getManager(),
84+
ToCharRange(SourceRange));
6785
return;
6886
}
6987
assert(Error.Message.Message.empty() && "Overwriting a diagnostic message");
7088
Error.Message = TidyMessage;
71-
for (const CharSourceRange &SourceRange : Ranges) {
72-
Error.Ranges.emplace_back(Loc.getManager(), SourceRange);
73-
}
89+
for (const CharSourceRange &SourceRange : Ranges)
90+
Error.Message.Ranges.emplace_back(Loc.getManager(),
91+
ToCharRange(SourceRange));
7492
}
7593

7694
void emitDiagnosticLoc(FullSourceLoc Loc, PresumedLoc PLoc,

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

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -213,6 +213,9 @@ class FunctionASTVisitor final
213213
: public RecursiveASTVisitor<FunctionASTVisitor> {
214214
using Base = RecursiveASTVisitor<FunctionASTVisitor>;
215215

216+
// If set to true, macros are ignored during analysis.
217+
const bool IgnoreMacros;
218+
216219
// The current nesting level (increased by Criteria::IncrementNesting).
217220
unsigned short CurrentNestingLevel = 0;
218221

@@ -223,6 +226,9 @@ class FunctionASTVisitor final
223226
std::stack<OBO, SmallVector<OBO, 4>> BinaryOperatorsStack;
224227

225228
public:
229+
explicit FunctionASTVisitor(const bool IgnoreMacros)
230+
: IgnoreMacros(IgnoreMacros) {}
231+
226232
bool traverseStmtWithIncreasedNestingLevel(Stmt *Node) {
227233
++CurrentNestingLevel;
228234
bool ShouldContinue = Base::TraverseStmt(Node);
@@ -364,6 +370,9 @@ class FunctionASTVisitor final
364370
if (!Node)
365371
return Base::TraverseStmt(Node);
366372

373+
if (IgnoreMacros && Node->getBeginLoc().isMacroID())
374+
return true;
375+
367376
// Three following switch()'es have huge duplication, but it is better to
368377
// keep them separate, to simplify comparing them with the Specification.
369378

@@ -493,12 +502,14 @@ FunctionCognitiveComplexityCheck::FunctionCognitiveComplexityCheck(
493502
StringRef Name, ClangTidyContext *Context)
494503
: ClangTidyCheck(Name, Context),
495504
Threshold(Options.get("Threshold", CognitiveComplexity::DefaultLimit)),
496-
DescribeBasicIncrements(Options.get("DescribeBasicIncrements", true)) {}
505+
DescribeBasicIncrements(Options.get("DescribeBasicIncrements", true)),
506+
IgnoreMacros(Options.get("IgnoreMacros", false)) {}
497507

498508
void FunctionCognitiveComplexityCheck::storeOptions(
499509
ClangTidyOptions::OptionMap &Opts) {
500510
Options.store(Opts, "Threshold", Threshold);
501511
Options.store(Opts, "DescribeBasicIncrements", DescribeBasicIncrements);
512+
Options.store(Opts, "IgnoreMacros", IgnoreMacros);
502513
}
503514

504515
void FunctionCognitiveComplexityCheck::registerMatchers(MatchFinder *Finder) {
@@ -513,7 +524,7 @@ void FunctionCognitiveComplexityCheck::registerMatchers(MatchFinder *Finder) {
513524
void FunctionCognitiveComplexityCheck::check(
514525
const MatchFinder::MatchResult &Result) {
515526

516-
FunctionASTVisitor Visitor;
527+
FunctionASTVisitor Visitor(IgnoreMacros);
517528
SourceLocation Loc;
518529

519530
const auto *TheDecl = Result.Nodes.getNodeAs<FunctionDecl>("func");

clang-tools-extra/clang-tidy/readability/FunctionCognitiveComplexityCheck.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,8 @@ namespace readability {
2626
/// diagnostics on every piece of code (loop, `if` statement, etc.) which
2727
/// contributes to that complexity.
2828
// Default is `true`
29+
/// * `IgnoreMacros` - if set to `true`, the check will ignore code inside
30+
/// macros. Default is `false`.
2931
///
3032
/// For the user-facing documentation see:
3133
/// http://clang.llvm.org/extra/clang-tidy/checks/readability-function-cognitive-complexity.html
@@ -43,6 +45,7 @@ class FunctionCognitiveComplexityCheck : public ClangTidyCheck {
4345
private:
4446
const unsigned Threshold;
4547
const bool DescribeBasicIncrements;
48+
const bool IgnoreMacros;
4649
};
4750

4851
} // namespace readability

clang-tools-extra/clangd/Config.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ struct Config {
7070
enum class BackgroundPolicy { Build, Skip };
7171
/// Describes an external index configuration.
7272
struct ExternalIndexSpec {
73-
enum { File, Server } Kind;
73+
enum { None, File, Server } Kind;
7474
/// This is one of:
7575
/// - Address of a clangd-index-server, in the form of "ip:port".
7676
/// - Absolute path to an index produced by clangd-indexer.

clang-tools-extra/clangd/ConfigCompile.cpp

Lines changed: 27 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -322,16 +322,18 @@ struct FragmentCompiler {
322322
llvm::SMRange BlockRange) {
323323
#ifndef CLANGD_ENABLE_REMOTE
324324
if (External.Server) {
325-
diag(Error, "Clangd isn't compiled with remote index support, ignoring "
326-
"Server." External.Server->Range);
325+
elog("Clangd isn't compiled with remote index support, ignoring Server: "
326+
"{0}",
327+
*External.Server);
327328
External.Server.reset();
328329
}
329330
#endif
330331
// Make sure exactly one of the Sources is set.
331-
unsigned SourceCount =
332-
External.File.hasValue() + External.Server.hasValue();
332+
unsigned SourceCount = External.File.hasValue() +
333+
External.Server.hasValue() + *External.IsNone;
333334
if (SourceCount != 1) {
334-
diag(Error, "Exactly one of File or Server must be set.", BlockRange);
335+
diag(Error, "Exactly one of File, Server or None must be set.",
336+
BlockRange);
335337
return;
336338
}
337339
Config::ExternalIndexSpec Spec;
@@ -345,20 +347,29 @@ struct FragmentCompiler {
345347
if (!AbsPath)
346348
return;
347349
Spec.Location = std::move(*AbsPath);
350+
} else {
351+
assert(*External.IsNone);
352+
Spec.Kind = Config::ExternalIndexSpec::None;
348353
}
349-
// Make sure MountPoint is an absolute path with forward slashes.
350-
if (!External.MountPoint)
351-
External.MountPoint.emplace(FragmentDirectory);
352-
if ((**External.MountPoint).empty()) {
353-
diag(Error, "A mountpoint is required.", BlockRange);
354-
return;
354+
if (Spec.Kind != Config::ExternalIndexSpec::None) {
355+
// Make sure MountPoint is an absolute path with forward slashes.
356+
if (!External.MountPoint)
357+
External.MountPoint.emplace(FragmentDirectory);
358+
if ((**External.MountPoint).empty()) {
359+
diag(Error, "A mountpoint is required.", BlockRange);
360+
return;
361+
}
362+
auto AbsPath = makeAbsolute(std::move(*External.MountPoint), "MountPoint",
363+
llvm::sys::path::Style::posix);
364+
if (!AbsPath)
365+
return;
366+
Spec.MountPoint = std::move(*AbsPath);
355367
}
356-
auto AbsPath = makeAbsolute(std::move(*External.MountPoint), "MountPoint",
357-
llvm::sys::path::Style::posix);
358-
if (!AbsPath)
359-
return;
360-
Spec.MountPoint = std::move(*AbsPath);
361368
Out.Apply.push_back([Spec(std::move(Spec))](const Params &P, Config &C) {
369+
if (Spec.Kind == Config::ExternalIndexSpec::None) {
370+
C.Index.External.reset();
371+
return;
372+
}
362373
if (P.Path.empty() || !pathStartsWith(Spec.MountPoint, P.Path,
363374
llvm::sys::path::Style::posix))
364375
return;

clang-tools-extra/clangd/ConfigFragment.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -173,6 +173,9 @@ struct Fragment {
173173
/// usually prepared using clangd-indexer.
174174
/// Exactly one source (File/Server) should be configured.
175175
struct ExternalBlock {
176+
/// Whether the block is explicitly set to `None`. Can be used to clear
177+
/// any external index specified before.
178+
Located<bool> IsNone = false;
176179
/// Path to an index file generated by clangd-indexer. Relative paths may
177180
/// be used, if config fragment is associated with a directory.
178181
llvm::Optional<Located<std::string>> File;

clang-tools-extra/clangd/ConfigYAML.cpp

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
#include "llvm/Support/MemoryBuffer.h"
1313
#include "llvm/Support/SourceMgr.h"
1414
#include "llvm/Support/YAMLParser.h"
15+
#include <string>
1516
#include <system_error>
1617

1718
namespace clang {
@@ -149,13 +150,34 @@ class Parser {
149150
[&](Node &N) { F.Background = scalarValue(N, "Background"); });
150151
Dict.handle("External", [&](Node &N) {
151152
Fragment::IndexBlock::ExternalBlock External;
152-
parse(External, N);
153+
// External block can either be a mapping or a scalar value. Dispatch
154+
// accordingly.
155+
if (N.getType() == Node::NK_Mapping) {
156+
parse(External, N);
157+
} else if (N.getType() == Node::NK_Scalar ||
158+
N.getType() == Node::NK_BlockScalar) {
159+
parse(External, scalarValue(N, "External").getValue());
160+
} else {
161+
error("External must be either a scalar or a mapping.", N);
162+
return;
163+
}
153164
F.External.emplace(std::move(External));
154165
F.External->Range = N.getSourceRange();
155166
});
156167
Dict.parse(N);
157168
}
158169

170+
void parse(Fragment::IndexBlock::ExternalBlock &F,
171+
Located<std::string> ExternalVal) {
172+
if (!llvm::StringRef(*ExternalVal).equals_lower("none")) {
173+
error("Only scalar value supported for External is 'None'",
174+
ExternalVal.Range);
175+
return;
176+
}
177+
F.IsNone = true;
178+
F.IsNone.Range = ExternalVal.Range;
179+
}
180+
159181
void parse(Fragment::IndexBlock::ExternalBlock &F, Node &N) {
160182
DictParser Dict("External", this);
161183
Dict.handle("File", [&](Node &N) { F.File = scalarValue(N, "File"); });
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
// RUN: cp %s %t.cpp
2+
// RUN: not clangd -check=%t.cpp -check-lines=6-14 2>&1 | FileCheck -strict-whitespace %s
3+
// RUN: not clangd -check=%t.cpp -check-lines=14 2>&1 | FileCheck -strict-whitespace %s
4+
5+
// CHECK: Testing on source file {{.*}}check-lines.test
6+
// CHECK: internal (cc1) args are: -cc1
7+
// CHECK: Building preamble...
8+
// CHECK: Building AST...
9+
// CHECK: Testing features at each token
10+
// CHECK: tweak: ExpandAutoType ==> FAIL
11+
// CHECK: All checks completed, 1 errors
12+
13+
void fun();
14+
auto x = fun; // This line is tested
15+
auto y = fun; // This line is not tested

clang-tools-extra/clangd/tool/Check.cpp

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -192,14 +192,19 @@ class Checker {
192192
}
193193

194194
// Run AST-based features at each token in the file.
195-
void testLocationFeatures() {
195+
void testLocationFeatures(
196+
llvm::function_ref<bool(const Position &)> ShouldCheckLine) {
196197
log("Testing features at each token (may be slow in large files)");
197-
auto SpelledTokens =
198-
AST->getTokens().spelledTokens(AST->getSourceManager().getMainFileID());
198+
auto &SM = AST->getSourceManager();
199+
auto SpelledTokens = AST->getTokens().spelledTokens(SM.getMainFileID());
199200
for (const auto &Tok : SpelledTokens) {
200201
unsigned Start = AST->getSourceManager().getFileOffset(Tok.location());
201202
unsigned End = Start + Tok.length();
202203
Position Pos = offsetToPosition(Inputs.Contents, Start);
204+
205+
if (!ShouldCheckLine(Pos))
206+
continue;
207+
203208
// FIXME: dumping the tokens may leak sensitive code into bug reports.
204209
// Add an option to turn this off, once we decide how options work.
205210
vlog(" {0} {1}", Pos, Tok.text(AST->getSourceManager()));
@@ -229,8 +234,9 @@ class Checker {
229234

230235
} // namespace
231236

232-
bool check(llvm::StringRef File, const ThreadsafeFS &TFS,
233-
const ClangdLSPServer::Options &Opts) {
237+
bool check(llvm::StringRef File,
238+
llvm::function_ref<bool(const Position &)> ShouldCheckLine,
239+
const ThreadsafeFS &TFS, const ClangdLSPServer::Options &Opts) {
234240
llvm::SmallString<0> FakeFile;
235241
llvm::Optional<std::string> Contents;
236242
if (File.empty()) {
@@ -254,7 +260,7 @@ bool check(llvm::StringRef File, const ThreadsafeFS &TFS,
254260
if (!C.buildCommand(TFS) || !C.buildInvocation(TFS, Contents) ||
255261
!C.buildAST())
256262
return false;
257-
C.testLocationFeatures();
263+
C.testLocationFeatures(ShouldCheckLine);
258264

259265
log("All checks completed, {0} errors", C.ErrCount);
260266
return C.ErrCount == 0;

0 commit comments

Comments
 (0)