Skip to content

Commit 75af1d6

Browse files
committed
Merge commit '9aae3dd94c881a36998ee8027d3e126ff3becf89' into llvmspirv_pulldown46
2 parents b20c31d + 9aae3dd commit 75af1d6

File tree

535 files changed

+13817
-4340
lines changed

Some content is hidden

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

535 files changed

+13817
-4340
lines changed

clang-tools-extra/clang-tidy/tool/ClangTidyMain.cpp

Lines changed: 7 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -519,9 +519,9 @@ int clangTidyMain(int argc, const char **argv) {
519519
std::vector<clang::tidy::ClangTidyOptionsProvider::OptionsSource>
520520
RawOptions = OptionsProvider->getRawOptions(FilePath);
521521
for (const std::string &Check : EnabledChecks) {
522-
for (auto It = RawOptions.rbegin(); It != RawOptions.rend(); ++It) {
523-
if (It->first.Checks && GlobList(*It->first.Checks).contains(Check)) {
524-
llvm::outs() << "'" << Check << "' is enabled in the " << It->second
522+
for (const auto &[Opts, Source] : llvm::reverse(RawOptions)) {
523+
if (Opts.Checks && GlobList(*Opts.Checks).contains(Check)) {
524+
llvm::outs() << "'" << Check << "' is enabled in the " << Source
525525
<< ".\n";
526526
break;
527527
}
@@ -557,20 +557,16 @@ int clangTidyMain(int argc, const char **argv) {
557557
NamesAndOptions Valid =
558558
getAllChecksAndOptions(AllowEnablingAnalyzerAlphaCheckers);
559559
bool AnyInvalid = false;
560-
for (const std::pair<ClangTidyOptions, std::string> &OptionWithSource :
561-
RawOptions) {
562-
const ClangTidyOptions &Opts = OptionWithSource.first;
560+
for (const auto &[Opts, Source] : RawOptions) {
563561
if (Opts.Checks)
564-
AnyInvalid |=
565-
verifyChecks(Valid.Names, *Opts.Checks, OptionWithSource.second);
562+
AnyInvalid |= verifyChecks(Valid.Names, *Opts.Checks, Source);
566563

567564
for (auto Key : Opts.CheckOptions.keys()) {
568565
if (Valid.Options.contains(Key))
569566
continue;
570567
AnyInvalid = true;
571-
auto &Output =
572-
llvm::WithColor::warning(llvm::errs(), OptionWithSource.second)
573-
<< "unknown check option '" << Key << '\'';
568+
auto &Output = llvm::WithColor::warning(llvm::errs(), Source)
569+
<< "unknown check option '" << Key << '\'';
574570
llvm::StringRef Closest = closest(Key, Valid.Options);
575571
if (!Closest.empty())
576572
Output << "; did you mean '" << Closest << '\'';

clang-tools-extra/clangd/CMakeLists.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,7 @@ add_clang_library(clangDaemon
9797
SemanticHighlighting.cpp
9898
SemanticSelection.cpp
9999
SourceCode.cpp
100-
QueryDriverDatabase.cpp
100+
SystemIncludeExtractor.cpp
101101
TidyProvider.cpp
102102
TUScheduler.cpp
103103
URI.cpp

clang-tools-extra/clangd/ClangdLSPServer.cpp

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -502,14 +502,14 @@ void ClangdLSPServer::onInitialize(const InitializeParams &Params,
502502
CDBOpts.ContextProvider = Opts.ContextProvider;
503503
BaseCDB =
504504
std::make_unique<DirectoryBasedGlobalCompilationDatabase>(CDBOpts);
505-
BaseCDB = getQueryDriverDatabase(llvm::makeArrayRef(Opts.QueryDriverGlobs),
506-
std::move(BaseCDB));
507505
}
508506
auto Mangler = CommandMangler::detect();
507+
Mangler.SystemIncludeExtractor =
508+
getSystemIncludeExtractor(llvm::makeArrayRef(Opts.QueryDriverGlobs));
509509
if (Opts.ResourceDir)
510510
Mangler.ResourceDir = *Opts.ResourceDir;
511511
CDB.emplace(BaseCDB.get(), Params.initializationOptions.fallbackFlags,
512-
tooling::ArgumentsAdjuster(std::move(Mangler)));
512+
std::move(Mangler));
513513
{
514514
// Switch caller's context with LSPServer's background context. Since we
515515
// rather want to propagate information from LSPServer's context into the
@@ -1815,5 +1815,6 @@ void ClangdLSPServer::onSemanticsMaybeChanged(PathRef File) {
18151815
});
18161816
}
18171817
}
1818+
18181819
} // namespace clangd
18191820
} // namespace clang

clang-tools-extra/clangd/CompileCommands.cpp

Lines changed: 14 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@
1313
#include "clang/Driver/Driver.h"
1414
#include "clang/Driver/Options.h"
1515
#include "clang/Frontend/CompilerInvocation.h"
16-
#include "clang/Tooling/ArgumentsAdjusters.h"
1716
#include "clang/Tooling/CompilationDatabase.h"
1817
#include "llvm/ADT/ArrayRef.h"
1918
#include "llvm/ADT/STLExtras.h"
@@ -195,8 +194,9 @@ CommandMangler CommandMangler::detect() {
195194

196195
CommandMangler CommandMangler::forTests() { return CommandMangler(); }
197196

198-
void CommandMangler::adjust(std::vector<std::string> &Cmd,
199-
llvm::StringRef File) const {
197+
void CommandMangler::operator()(tooling::CompileCommand &Command,
198+
llvm::StringRef File) const {
199+
std::vector<std::string> &Cmd = Command.CommandLine;
200200
trace::Span S("AdjustCompileFlags");
201201
// Most of the modifications below assumes the Cmd starts with a driver name.
202202
// We might consider injecting a generic driver name like "cc" or "c++", but
@@ -301,6 +301,17 @@ void CommandMangler::adjust(std::vector<std::string> &Cmd,
301301
for (auto &Edit : Config::current().CompileFlags.Edits)
302302
Edit(Cmd);
303303

304+
// The system include extractor needs to run:
305+
// - AFTER transferCompileCommand(), because the -x flag it adds may be
306+
// necessary for the system include extractor to identify the file type
307+
// - AFTER applying CompileFlags.Edits, because the name of the compiler
308+
// that needs to be invoked may come from the CompileFlags->Compiler key
309+
// - BEFORE resolveDriver() because that can mess up the driver path,
310+
// e.g. changing gcc to /path/to/clang/bin/gcc
311+
if (SystemIncludeExtractor) {
312+
SystemIncludeExtractor(Command, File);
313+
}
314+
304315
// Check whether the flag exists, either as -flag or -flag=*
305316
auto Has = [&](llvm::StringRef Flag) {
306317
for (llvm::StringRef Arg : Cmd) {
@@ -340,16 +351,6 @@ void CommandMangler::adjust(std::vector<std::string> &Cmd,
340351
}
341352
}
342353

343-
CommandMangler::operator clang::tooling::ArgumentsAdjuster() && {
344-
// ArgumentsAdjuster is a std::function and so must be copyable.
345-
return [Mangler = std::make_shared<CommandMangler>(std::move(*this))](
346-
const std::vector<std::string> &Args, llvm::StringRef File) {
347-
auto Result = Args;
348-
Mangler->adjust(Result, File);
349-
return Result;
350-
};
351-
}
352-
353354
// ArgStripper implementation
354355
namespace {
355356

clang-tools-extra/clangd/CompileCommands.h

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,8 @@
88
#ifndef LLVM_CLANG_TOOLS_EXTRA_CLANGD_COMPILECOMMANDS_H
99
#define LLVM_CLANG_TOOLS_EXTRA_CLANGD_COMPILECOMMANDS_H
1010

11+
#include "GlobalCompilationDatabase.h"
1112
#include "support/Threading.h"
12-
#include "clang/Tooling/ArgumentsAdjusters.h"
1313
#include "llvm/ADT/StringMap.h"
1414
#include "llvm/ADT/StringRef.h"
1515
#include <deque>
@@ -32,6 +32,7 @@ struct CommandMangler {
3232
llvm::Optional<std::string> ResourceDir;
3333
// Root for searching for standard library (passed to -isysroot).
3434
llvm::Optional<std::string> Sysroot;
35+
SystemIncludeExtractorFn SystemIncludeExtractor;
3536

3637
// A command-mangler that doesn't know anything about the system.
3738
// This is hermetic for unit-tests, but won't work well in production.
@@ -42,11 +43,14 @@ struct CommandMangler {
4243
// - on mac, find clang and isysroot by querying the `xcrun` launcher
4344
static CommandMangler detect();
4445

45-
void adjust(std::vector<std::string> &Cmd, llvm::StringRef File) const;
46-
explicit operator clang::tooling::ArgumentsAdjuster() &&;
46+
// `Cmd` may describe compilation of a different file, and will be updated
47+
// for parsing `TargetFile`.
48+
void operator()(tooling::CompileCommand &Cmd,
49+
llvm::StringRef TargetFile) const;
4750

4851
private:
4952
CommandMangler() = default;
53+
5054
Memoize<llvm::StringMap<std::string>> ResolvedDrivers;
5155
Memoize<llvm::StringMap<std::string>> ResolvedDriversNoFollow;
5256
};

clang-tools-extra/clangd/GlobalCompilationDatabase.cpp

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -740,8 +740,8 @@ DirectoryBasedGlobalCompilationDatabase::getProjectInfo(PathRef File) const {
740740

741741
OverlayCDB::OverlayCDB(const GlobalCompilationDatabase *Base,
742742
std::vector<std::string> FallbackFlags,
743-
tooling::ArgumentsAdjuster Adjuster)
744-
: DelegatingCDB(Base), ArgsAdjuster(std::move(Adjuster)),
743+
CommandMangler Mangler)
744+
: DelegatingCDB(Base), Mangler(std::move(Mangler)),
745745
FallbackFlags(std::move(FallbackFlags)) {}
746746

747747
llvm::Optional<tooling::CompileCommand>
@@ -757,8 +757,8 @@ OverlayCDB::getCompileCommand(PathRef File) const {
757757
Cmd = DelegatingCDB::getCompileCommand(File);
758758
if (!Cmd)
759759
return llvm::None;
760-
if (ArgsAdjuster)
761-
Cmd->CommandLine = ArgsAdjuster(Cmd->CommandLine, File);
760+
if (Mangler)
761+
Mangler(*Cmd, File);
762762
return Cmd;
763763
}
764764

@@ -767,8 +767,8 @@ tooling::CompileCommand OverlayCDB::getFallbackCommand(PathRef File) const {
767767
std::lock_guard<std::mutex> Lock(Mutex);
768768
Cmd.CommandLine.insert(Cmd.CommandLine.end(), FallbackFlags.begin(),
769769
FallbackFlags.end());
770-
if (ArgsAdjuster)
771-
Cmd.CommandLine = ArgsAdjuster(Cmd.CommandLine, File);
770+
if (Mangler)
771+
Mangler(Cmd, File);
772772
return Cmd;
773773
}
774774

clang-tools-extra/clangd/GlobalCompilationDatabase.h

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
#include "support/ThreadsafeFS.h"
1616
#include "clang/Tooling/ArgumentsAdjusters.h"
1717
#include "clang/Tooling/CompilationDatabase.h"
18+
#include "llvm/ADT/FunctionExtras.h"
1819
#include "llvm/ADT/Optional.h"
1920
#include "llvm/ADT/StringMap.h"
2021
#include <memory>
@@ -161,22 +162,28 @@ class DirectoryBasedGlobalCompilationDatabase
161162
};
162163

163164
/// Extracts system include search path from drivers matching QueryDriverGlobs
164-
/// and adds them to the compile flags. Base may not be nullptr.
165-
/// Returns Base when \p QueryDriverGlobs is empty.
166-
std::unique_ptr<GlobalCompilationDatabase>
167-
getQueryDriverDatabase(llvm::ArrayRef<std::string> QueryDriverGlobs,
168-
std::unique_ptr<GlobalCompilationDatabase> Base);
165+
/// and adds them to the compile flags.
166+
/// Returns null when \p QueryDriverGlobs is empty.
167+
using SystemIncludeExtractorFn = llvm::unique_function<void(
168+
tooling::CompileCommand &, llvm::StringRef) const>;
169+
SystemIncludeExtractorFn
170+
getSystemIncludeExtractor(llvm::ArrayRef<std::string> QueryDriverGlobs);
169171

170172
/// Wraps another compilation database, and supports overriding the commands
171173
/// using an in-memory mapping.
172174
class OverlayCDB : public DelegatingCDB {
173175
public:
176+
// Makes adjustments to a tooling::CompileCommand which will be used to
177+
// process a file (possibly different from the one in the command).
178+
using CommandMangler = llvm::unique_function<void(tooling::CompileCommand &,
179+
StringRef File) const>;
180+
174181
// Base may be null, in which case no entries are inherited.
175182
// FallbackFlags are added to the fallback compile command.
176183
// Adjuster is applied to all commands, fallback or not.
177184
OverlayCDB(const GlobalCompilationDatabase *Base,
178185
std::vector<std::string> FallbackFlags = {},
179-
tooling::ArgumentsAdjuster Adjuster = nullptr);
186+
CommandMangler Mangler = nullptr);
180187

181188
llvm::Optional<tooling::CompileCommand>
182189
getCompileCommand(PathRef File) const override;
@@ -190,7 +197,7 @@ class OverlayCDB : public DelegatingCDB {
190197
private:
191198
mutable std::mutex Mutex;
192199
llvm::StringMap<tooling::CompileCommand> Commands; /* GUARDED_BY(Mut) */
193-
tooling::ArgumentsAdjuster ArgsAdjuster;
200+
CommandMangler Mangler;
194201
std::vector<std::string> FallbackFlags;
195202
};
196203

clang-tools-extra/clangd/SemanticHighlighting.cpp

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -597,27 +597,36 @@ class CollectExtraHighlightings
597597
if (!Arg)
598598
return;
599599

600-
// Is this parameter passed by non-const reference?
600+
// Is this parameter passed by non-const pointer or reference?
601601
// FIXME The condition T->idDependentType() could be relaxed a bit,
602602
// e.g. std::vector<T>& is dependent but we would want to highlight it
603-
if (!T->isLValueReferenceType() ||
604-
T.getNonReferenceType().isConstQualified() || T->isDependentType()) {
603+
bool IsRef = T->isLValueReferenceType();
604+
bool IsPtr = T->isPointerType();
605+
if ((!IsRef && !IsPtr) || T->getPointeeType().isConstQualified() ||
606+
T->isDependentType()) {
605607
return;
606608
}
607609

608610
llvm::Optional<SourceLocation> Location;
609611

610-
// FIXME Add "unwrapping" for ArraySubscriptExpr and UnaryOperator,
612+
// FIXME Add "unwrapping" for ArraySubscriptExpr,
611613
// e.g. highlight `a` in `a[i]`
612614
// FIXME Handle dependent expression types
615+
if (auto *IC = dyn_cast<ImplicitCastExpr>(Arg))
616+
Arg = IC->getSubExprAsWritten();
617+
if (auto *UO = dyn_cast<UnaryOperator>(Arg)) {
618+
if (UO->getOpcode() == UO_AddrOf)
619+
Arg = UO->getSubExpr();
620+
}
613621
if (auto *DR = dyn_cast<DeclRefExpr>(Arg))
614622
Location = DR->getLocation();
615623
else if (auto *M = dyn_cast<MemberExpr>(Arg))
616624
Location = M->getMemberLoc();
617625

618626
if (Location)
619627
H.addExtraModifier(*Location,
620-
HighlightingModifier::UsedAsMutableReference);
628+
IsRef ? HighlightingModifier::UsedAsMutableReference
629+
: HighlightingModifier::UsedAsMutablePointer);
621630
}
622631

623632
void
@@ -1140,6 +1149,8 @@ llvm::StringRef toSemanticTokenModifier(HighlightingModifier Modifier) {
11401149
return "defaultLibrary";
11411150
case HighlightingModifier::UsedAsMutableReference:
11421151
return "usedAsMutableReference"; // nonstandard
1152+
case HighlightingModifier::UsedAsMutablePointer:
1153+
return "usedAsMutablePointer"; // nonstandard
11431154
case HighlightingModifier::ConstructorOrDestructor:
11441155
return "constructorOrDestructor"; // nonstandard
11451156
case HighlightingModifier::FunctionScope:

clang-tools-extra/clangd/SemanticHighlighting.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,7 @@ enum class HighlightingModifier {
7171
DependentName,
7272
DefaultLibrary,
7373
UsedAsMutableReference,
74+
UsedAsMutablePointer,
7475
ConstructorOrDestructor,
7576

7677
FunctionScope,

clang-tools-extra/clangd/QueryDriverDatabase.cpp renamed to clang-tools-extra/clangd/SystemIncludeExtractor.cpp

Lines changed: 20 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
//===--- QueryDriverDatabase.cpp ---------------------------------*- C++-*-===//
1+
//===--- SystemIncludeExtractor.cpp ------------------------------*- C++-*-===//
22
//
33
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
44
// See https://llvm.org/LICENSE.txt for license information.
@@ -315,24 +315,20 @@ llvm::Regex convertGlobsToRegex(llvm::ArrayRef<std::string> Globs) {
315315
/// Extracts system includes from a trusted driver by parsing the output of
316316
/// include search path and appends them to the commands coming from underlying
317317
/// compilation database.
318-
class QueryDriverDatabase : public DelegatingCDB {
318+
class SystemIncludeExtractor {
319319
public:
320-
QueryDriverDatabase(llvm::ArrayRef<std::string> QueryDriverGlobs,
321-
std::unique_ptr<GlobalCompilationDatabase> Base)
322-
: DelegatingCDB(std::move(Base)),
323-
QueryDriverRegex(convertGlobsToRegex(QueryDriverGlobs)) {}
320+
SystemIncludeExtractor(llvm::ArrayRef<std::string> QueryDriverGlobs)
321+
: QueryDriverRegex(convertGlobsToRegex(QueryDriverGlobs)) {}
324322

325-
llvm::Optional<tooling::CompileCommand>
326-
getCompileCommand(PathRef File) const override {
327-
auto Cmd = DelegatingCDB::getCompileCommand(File);
328-
if (!Cmd || Cmd->CommandLine.empty())
329-
return Cmd;
323+
void operator()(tooling::CompileCommand &Cmd, llvm::StringRef File) const {
324+
if (Cmd.CommandLine.empty())
325+
return;
330326

331327
llvm::StringRef Lang;
332-
for (size_t I = 0, E = Cmd->CommandLine.size(); I < E; ++I) {
333-
llvm::StringRef Arg = Cmd->CommandLine[I];
328+
for (size_t I = 0, E = Cmd.CommandLine.size(); I < E; ++I) {
329+
llvm::StringRef Arg = Cmd.CommandLine[I];
334330
if (Arg == "-x" && I + 1 < E)
335-
Lang = Cmd->CommandLine[I + 1];
331+
Lang = Cmd.CommandLine[I + 1];
336332
else if (Arg.startswith("-x"))
337333
Lang = Arg.drop_front(2).trim();
338334
}
@@ -341,26 +337,25 @@ class QueryDriverDatabase : public DelegatingCDB {
341337
auto Type = driver::types::lookupTypeForExtension(Ext);
342338
if (Type == driver::types::TY_INVALID) {
343339
elog("System include extraction: invalid file type for {0}", Ext);
344-
return Cmd;
340+
return;
345341
}
346342
Lang = driver::types::getTypeName(Type);
347343
}
348344

349-
llvm::SmallString<128> Driver(Cmd->CommandLine.front());
345+
llvm::SmallString<128> Driver(Cmd.CommandLine.front());
350346
if (llvm::any_of(Driver,
351-
[](char C) { return llvm::sys::path::is_separator(C); }))
347+
[](char C) { return llvm::sys::path::is_separator(C); }))
352348
// Driver is a not a single executable name but instead a path (either
353349
// relative or absolute).
354-
llvm::sys::fs::make_absolute(Cmd->Directory, Driver);
350+
llvm::sys::fs::make_absolute(Cmd.Directory, Driver);
355351

356352
if (auto Info =
357353
QueriedDrivers.get(/*Key=*/(Driver + ":" + Lang).str(), [&] {
358354
return extractSystemIncludesAndTarget(
359-
Driver, Lang, Cmd->CommandLine, QueryDriverRegex);
355+
Driver, Lang, Cmd.CommandLine, QueryDriverRegex);
360356
})) {
361-
setTarget(addSystemIncludes(*Cmd, Info->SystemIncludes), Info->Target);
357+
setTarget(addSystemIncludes(Cmd, Info->SystemIncludes), Info->Target);
362358
}
363-
return Cmd;
364359
}
365360

366361
private:
@@ -370,14 +365,11 @@ class QueryDriverDatabase : public DelegatingCDB {
370365
};
371366
} // namespace
372367

373-
std::unique_ptr<GlobalCompilationDatabase>
374-
getQueryDriverDatabase(llvm::ArrayRef<std::string> QueryDriverGlobs,
375-
std::unique_ptr<GlobalCompilationDatabase> Base) {
376-
assert(Base && "Null base to SystemIncludeExtractor");
368+
SystemIncludeExtractorFn
369+
getSystemIncludeExtractor(llvm::ArrayRef<std::string> QueryDriverGlobs) {
377370
if (QueryDriverGlobs.empty())
378-
return Base;
379-
return std::make_unique<QueryDriverDatabase>(QueryDriverGlobs,
380-
std::move(Base));
371+
return nullptr;
372+
return SystemIncludeExtractor(QueryDriverGlobs);
381373
}
382374

383375
} // namespace clangd

0 commit comments

Comments
 (0)