Skip to content

Commit 897f832

Browse files
Merge branch 'swift/master-next' into class-constrained-protocol-eval-next
2 parents fea5ac2 + 692207c commit 897f832

File tree

461 files changed

+18036
-2745
lines changed

Some content is hidden

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

461 files changed

+18036
-2745
lines changed

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

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -155,12 +155,19 @@ void ClangTidyCheck::OptionsView::store(ClangTidyOptions::OptionMap &Options,
155155
Options[NamePrefix + LocalName.str()] = Value;
156156
}
157157

158-
void ClangTidyCheck::OptionsView::store(ClangTidyOptions::OptionMap &Options,
159-
StringRef LocalName,
160-
int64_t Value) const {
158+
void ClangTidyCheck::OptionsView::storeInt(ClangTidyOptions::OptionMap &Options,
159+
StringRef LocalName,
160+
int64_t Value) const {
161161
store(Options, LocalName, llvm::itostr(Value));
162162
}
163163

164+
template <>
165+
void ClangTidyCheck::OptionsView::store<bool>(
166+
ClangTidyOptions::OptionMap &Options, StringRef LocalName,
167+
bool Value) const {
168+
store(Options, LocalName, Value ? StringRef("true") : StringRef("false"));
169+
}
170+
164171
llvm::Expected<int64_t>
165172
ClangTidyCheck::OptionsView::getEnumInt(StringRef LocalName,
166173
ArrayRef<NameAndValue> Mapping,

clang-tools-extra/clang-tidy/ClangTidyCheck.h

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -405,9 +405,13 @@ class ClangTidyCheck : public ast_matchers::MatchFinder::MatchCallback {
405405
StringRef Value) const;
406406

407407
/// Stores an option with the check-local name \p LocalName with
408-
/// ``int64_t`` value \p Value to \p Options.
409-
void store(ClangTidyOptions::OptionMap &Options, StringRef LocalName,
410-
int64_t Value) const;
408+
/// integer value \p Value to \p Options.
409+
template <typename T>
410+
std::enable_if_t<std::is_integral<T>::value>
411+
store(ClangTidyOptions::OptionMap &Options, StringRef LocalName,
412+
T Value) const {
413+
storeInt(Options, LocalName, Value);
414+
}
411415

412416
/// Stores an option with the check-local name \p LocalName as the string
413417
/// representation of the Enum \p Value to \p Options.
@@ -448,6 +452,9 @@ class ClangTidyCheck : public ast_matchers::MatchFinder::MatchCallback {
448452
return Result;
449453
}
450454

455+
void storeInt(ClangTidyOptions::OptionMap &Options, StringRef LocalName,
456+
int64_t Value) const;
457+
451458
static void logErrToStdErr(llvm::Error &&Err);
452459

453460
std::string NamePrefix;
@@ -509,6 +516,13 @@ template <>
509516
bool ClangTidyCheck::OptionsView::getLocalOrGlobal<bool>(StringRef LocalName,
510517
bool Default) const;
511518

519+
/// Stores an option with the check-local name \p LocalName with
520+
/// bool value \p Value to \p Options.
521+
template <>
522+
void ClangTidyCheck::OptionsView::store<bool>(
523+
ClangTidyOptions::OptionMap &Options, StringRef LocalName,
524+
bool Value) const;
525+
512526
} // namespace tidy
513527
} // namespace clang
514528

clang-tools-extra/clangd/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ set(LLVM_LINK_COMPONENTS
2525
Support
2626
AllTargetsInfos
2727
FrontendOpenMP
28+
Option
2829
)
2930

3031
add_clang_library(clangDaemon

clang-tools-extra/clangd/ClangdServer.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@
4848
#include "llvm/Support/ScopedPrinter.h"
4949
#include "llvm/Support/raw_ostream.h"
5050
#include <algorithm>
51+
#include <chrono>
5152
#include <future>
5253
#include <memory>
5354
#include <mutex>
@@ -762,6 +763,9 @@ Context ClangdServer::createProcessingContext(PathRef File) const {
762763
return Context::current().clone();
763764

764765
config::Params Params;
766+
// Don't reread config files excessively often.
767+
// FIXME: when we see a config file change event, use the event timestamp.
768+
Params.FreshTime = std::chrono::steady_clock::now() - std::chrono::seconds(5);
765769
llvm::SmallString<256> PosixPath;
766770
if (!File.empty()) {
767771
assert(llvm::sys::path::is_absolute(File));

clang-tools-extra/clangd/CompileCommands.h

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
#include "clang/Tooling/ArgumentsAdjusters.h"
1313
#include "clang/Tooling/CompilationDatabase.h"
1414
#include "llvm/ADT/StringMap.h"
15+
#include <deque>
1516
#include <string>
1617
#include <vector>
1718

@@ -59,6 +60,12 @@ struct CommandMangler {
5960
// The table-building strategy may not make sense outside clangd.
6061
class ArgStripper {
6162
public:
63+
ArgStripper() = default;
64+
ArgStripper(ArgStripper &&) = default;
65+
ArgStripper(const ArgStripper &) = delete;
66+
ArgStripper &operator=(ArgStripper &&) = default;
67+
ArgStripper &operator=(const ArgStripper &) = delete;
68+
6269
// Adds the arg to the set which should be removed.
6370
//
6471
// Recognized clang flags are stripped semantically. When "-I" is stripped:
@@ -86,7 +93,7 @@ class ArgStripper {
8693
const Rule *matchingRule(llvm::StringRef Arg, unsigned Mode,
8794
unsigned &ArgCount) const;
8895
llvm::SmallVector<Rule, 4> Rules;
89-
std::vector<std::string> Storage; // Store strings not found in option table.
96+
std::deque<std::string> Storage; // Store strings not found in option table.
9097
};
9198

9299
} // namespace clangd

clang-tools-extra/clangd/Config.h

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,13 @@ struct Config {
5555
std::vector<llvm::unique_function<void(std::vector<std::string> &) const>>
5656
Edits;
5757
} CompileFlags;
58+
59+
enum class BackgroundPolicy { Build, Skip };
60+
/// Controls background-index behavior.
61+
struct {
62+
/// Whether this TU should be indexed.
63+
BackgroundPolicy Background = BackgroundPolicy::Build;
64+
} Index;
5865
};
5966

6067
} // namespace clangd

clang-tools-extra/clangd/ConfigCompile.cpp

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,11 +23,14 @@
2323
//
2424
//===----------------------------------------------------------------------===//
2525

26+
#include "CompileCommands.h"
2627
#include "Config.h"
2728
#include "ConfigFragment.h"
2829
#include "support/Logger.h"
2930
#include "support/Trace.h"
31+
#include "llvm/ADT/STLExtras.h"
3032
#include "llvm/ADT/StringRef.h"
33+
#include "llvm/ADT/StringSwitch.h"
3134
#include "llvm/Support/Regex.h"
3235
#include "llvm/Support/SMLoc.h"
3336
#include "llvm/Support/SourceMgr.h"
@@ -79,9 +82,56 @@ struct FragmentCompiler {
7982
return Result;
8083
}
8184

85+
// Helper with similar API to StringSwitch, for parsing enum values.
86+
template <typename T> class EnumSwitch {
87+
FragmentCompiler &Outer;
88+
llvm::StringRef EnumName;
89+
const Located<std::string> &Input;
90+
llvm::Optional<T> Result;
91+
llvm::SmallVector<llvm::StringLiteral, 8> ValidValues;
92+
93+
public:
94+
EnumSwitch(llvm::StringRef EnumName, const Located<std::string> &In,
95+
FragmentCompiler &Outer)
96+
: Outer(Outer), EnumName(EnumName), Input(In) {}
97+
98+
EnumSwitch &map(llvm::StringLiteral Name, T Value) {
99+
assert(!llvm::is_contained(ValidValues, Name) && "Duplicate value!");
100+
ValidValues.push_back(Name);
101+
if (!Result && *Input == Name)
102+
Result = Value;
103+
return *this;
104+
}
105+
106+
llvm::Optional<T> value() {
107+
if (!Result)
108+
Outer.diag(
109+
Warning,
110+
llvm::formatv("Invalid {0} value '{1}'. Valid values are {2}.",
111+
EnumName, *Input, llvm::join(ValidValues, ", "))
112+
.str(),
113+
Input.Range);
114+
return Result;
115+
};
116+
};
117+
118+
// Attempt to parse a specified string into an enum.
119+
// Yields llvm::None and produces a diagnostic on failure.
120+
//
121+
// Optional<T> Value = compileEnum<En>("Foo", Frag.Foo)
122+
// .map("Foo", Enum::Foo)
123+
// .map("Bar", Enum::Bar)
124+
// .value();
125+
template <typename T>
126+
EnumSwitch<T> compileEnum(llvm::StringRef EnumName,
127+
const Located<std::string> &In) {
128+
return EnumSwitch<T>(EnumName, In, *this);
129+
}
130+
82131
void compile(Fragment &&F) {
83132
compile(std::move(F.If));
84133
compile(std::move(F.CompileFlags));
134+
compile(std::move(F.Index));
85135
}
86136

87137
void compile(Fragment::IfBlock &&F) {
@@ -122,6 +172,19 @@ struct FragmentCompiler {
122172
}
123173

124174
void compile(Fragment::CompileFlagsBlock &&F) {
175+
if (!F.Remove.empty()) {
176+
auto Remove = std::make_shared<ArgStripper>();
177+
for (auto &A : F.Remove)
178+
Remove->strip(*A);
179+
Out.Apply.push_back([Remove(std::shared_ptr<const ArgStripper>(
180+
std::move(Remove)))](Config &C) {
181+
C.CompileFlags.Edits.push_back(
182+
[Remove](std::vector<std::string> &Args) {
183+
Remove->process(Args);
184+
});
185+
});
186+
}
187+
125188
if (!F.Add.empty()) {
126189
std::vector<std::string> Add;
127190
for (auto &A : F.Add)
@@ -134,7 +197,20 @@ struct FragmentCompiler {
134197
}
135198
}
136199

200+
void compile(Fragment::IndexBlock &&F) {
201+
if (F.Background) {
202+
if (auto Val = compileEnum<Config::BackgroundPolicy>("Background",
203+
**F.Background)
204+
.map("Build", Config::BackgroundPolicy::Build)
205+
.map("Skip", Config::BackgroundPolicy::Skip)
206+
.value())
207+
Out.Apply.push_back([Val](Config &C) { C.Index.Background = *Val; });
208+
}
209+
}
210+
137211
constexpr static llvm::SourceMgr::DiagKind Error = llvm::SourceMgr::DK_Error;
212+
constexpr static llvm::SourceMgr::DiagKind Warning =
213+
llvm::SourceMgr::DK_Warning;
138214
void diag(llvm::SourceMgr::DiagKind Kind, llvm::StringRef Message,
139215
llvm::SMRange Range) {
140216
if (Range.isValid() && SourceMgr != nullptr)

clang-tools-extra/clangd/ConfigFragment.h

Lines changed: 42 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -117,9 +117,50 @@ struct Fragment {
117117
};
118118
IfBlock If;
119119

120+
/// Conditions in the CompileFlags block affect how a file is parsed.
121+
///
122+
/// clangd emulates how clang would interpret a file.
123+
/// By default, it behaves roughly like `clang $FILENAME`, but real projects
124+
/// usually require setting the include path (with the `-I` flag), defining
125+
/// preprocessor symbols, configuring warnings etc.
126+
/// Often, a compilation database specifies these compile commands. clangd
127+
/// searches for compile_commands.json in parents of the source file.
128+
///
129+
/// This section modifies how the compile command is constructed.
120130
struct CompileFlagsBlock {
131+
/// List of flags to append to the compile command.
121132
std::vector<Located<std::string>> Add;
122-
} CompileFlags;
133+
/// List of flags to remove from the compile command.
134+
///
135+
/// - If the value is a recognized clang flag (like "-I") then it will be
136+
/// removed along with any arguments. Synonyms like --include-dir= will
137+
/// also be removed.
138+
/// - Otherwise, if the value ends in * (like "-DFOO=*") then any argument
139+
/// with the prefix will be removed.
140+
/// - Otherwise any argument exactly matching the value is removed.
141+
///
142+
/// In all cases, -Xclang is also removed where needed.
143+
///
144+
/// Example:
145+
/// Command: clang++ --include-directory=/usr/include -DFOO=42 foo.cc
146+
/// Remove: [-I, -DFOO=*]
147+
/// Result: clang++ foo.cc
148+
///
149+
/// Flags added by the same CompileFlags entry will not be removed.
150+
std::vector<Located<std::string>> Remove;
151+
};
152+
CompileFlagsBlock CompileFlags;
153+
154+
/// Controls how clangd understands code outside the current file.
155+
/// clangd's indexes provide information about symbols that isn't available
156+
/// to clang's parser, such as incoming references.
157+
struct IndexBlock {
158+
/// Whether files are built in the background to produce a project index.
159+
/// This is checked for translation units only, not headers they include.
160+
/// Legal values are "Build" or "Skip".
161+
llvm::Optional<Located<std::string>> Background;
162+
};
163+
IndexBlock Index;
123164
};
124165

125166
} // namespace config

0 commit comments

Comments
 (0)