1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248
|
//===--- ConfigCompile.cpp - Translating Fragments into Config ------------===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//
//
// Fragments are applied to Configs in two steps:
//
// 1. (When the fragment is first loaded)
// FragmentCompiler::compile() traverses the Fragment and creates
// function objects that know how to apply the configuration.
// 2. (Every time a config is required)
// CompiledFragment() executes these functions to populate the Config.
//
// Work could be split between these steps in different ways. We try to
// do as much work as possible in the first step. For example, regexes are
// compiled in stage 1 and captured by the apply function. This is because:
//
// - it's more efficient, as the work done in stage 1 must only be done once
// - problems can be reported in stage 1, in stage 2 we must silently recover
//
//===----------------------------------------------------------------------===//
#include "CompileCommands.h"
#include "Config.h"
#include "ConfigFragment.h"
#include "support/Logger.h"
#include "support/Trace.h"
#include "llvm/ADT/STLExtras.h"
#include "llvm/ADT/StringRef.h"
#include "llvm/ADT/StringSwitch.h"
#include "llvm/Support/Regex.h"
#include "llvm/Support/SMLoc.h"
#include "llvm/Support/SourceMgr.h"
namespace clang {
namespace clangd {
namespace config {
namespace {
struct CompiledFragmentImpl {
// The independent conditions to check before using settings from this config.
// The following fragment has *two* conditions:
// If: { Platform: [mac, linux], PathMatch: foo/.* }
// All of them must be satisfied: the platform and path conditions are ANDed.
// The OR logic for the platform condition is implemented inside the function.
std::vector<llvm::unique_function<bool(const Params &) const>> Conditions;
// Mutations that this fragment will apply to the configuration.
// These are invoked only if the conditions are satisfied.
std::vector<llvm::unique_function<void(Config &) const>> Apply;
bool operator()(const Params &P, Config &C) const {
for (const auto &C : Conditions) {
if (!C(P)) {
dlog("Config fragment {0}: condition not met", this);
return false;
}
}
dlog("Config fragment {0}: applying {1} rules", this, Apply.size());
for (const auto &A : Apply)
A(C);
return true;
}
};
// Wrapper around condition compile() functions to reduce arg-passing.
struct FragmentCompiler {
CompiledFragmentImpl &Out;
DiagnosticCallback Diagnostic;
llvm::SourceMgr *SourceMgr;
llvm::Optional<llvm::Regex> compileRegex(const Located<std::string> &Text) {
std::string Anchored = "^(" + *Text + ")$";
llvm::Regex Result(Anchored);
std::string RegexError;
if (!Result.isValid(RegexError)) {
diag(Error, "Invalid regex " + Anchored + ": " + RegexError, Text.Range);
return llvm::None;
}
return Result;
}
// Helper with similar API to StringSwitch, for parsing enum values.
template <typename T> class EnumSwitch {
FragmentCompiler &Outer;
llvm::StringRef EnumName;
const Located<std::string> &Input;
llvm::Optional<T> Result;
llvm::SmallVector<llvm::StringLiteral, 8> ValidValues;
public:
EnumSwitch(llvm::StringRef EnumName, const Located<std::string> &In,
FragmentCompiler &Outer)
: Outer(Outer), EnumName(EnumName), Input(In) {}
EnumSwitch &map(llvm::StringLiteral Name, T Value) {
assert(!llvm::is_contained(ValidValues, Name) && "Duplicate value!");
ValidValues.push_back(Name);
if (!Result && *Input == Name)
Result = Value;
return *this;
}
llvm::Optional<T> value() {
if (!Result)
Outer.diag(
Warning,
llvm::formatv("Invalid {0} value '{1}'. Valid values are {2}.",
EnumName, *Input, llvm::join(ValidValues, ", "))
.str(),
Input.Range);
return Result;
};
};
// Attempt to parse a specified string into an enum.
// Yields llvm::None and produces a diagnostic on failure.
//
// Optional<T> Value = compileEnum<En>("Foo", Frag.Foo)
// .map("Foo", Enum::Foo)
// .map("Bar", Enum::Bar)
// .value();
template <typename T>
EnumSwitch<T> compileEnum(llvm::StringRef EnumName,
const Located<std::string> &In) {
return EnumSwitch<T>(EnumName, In, *this);
}
void compile(Fragment &&F) {
compile(std::move(F.If));
compile(std::move(F.CompileFlags));
compile(std::move(F.Index));
}
void compile(Fragment::IfBlock &&F) {
if (F.HasUnrecognizedCondition)
Out.Conditions.push_back([&](const Params &) { return false; });
auto PathMatch = std::make_unique<std::vector<llvm::Regex>>();
for (auto &Entry : F.PathMatch) {
if (auto RE = compileRegex(Entry))
PathMatch->push_back(std::move(*RE));
}
if (!PathMatch->empty()) {
Out.Conditions.push_back(
[PathMatch(std::move(PathMatch))](const Params &P) {
if (P.Path.empty())
return false;
return llvm::any_of(*PathMatch, [&](const llvm::Regex &RE) {
return RE.match(P.Path);
});
});
}
auto PathExclude = std::make_unique<std::vector<llvm::Regex>>();
for (auto &Entry : F.PathExclude) {
if (auto RE = compileRegex(Entry))
PathExclude->push_back(std::move(*RE));
}
if (!PathExclude->empty()) {
Out.Conditions.push_back(
[PathExclude(std::move(PathExclude))](const Params &P) {
if (P.Path.empty())
return false;
return llvm::none_of(*PathExclude, [&](const llvm::Regex &RE) {
return RE.match(P.Path);
});
});
}
}
void compile(Fragment::CompileFlagsBlock &&F) {
if (!F.Remove.empty()) {
auto Remove = std::make_shared<ArgStripper>();
for (auto &A : F.Remove)
Remove->strip(*A);
Out.Apply.push_back([Remove(std::shared_ptr<const ArgStripper>(
std::move(Remove)))](Config &C) {
C.CompileFlags.Edits.push_back(
[Remove](std::vector<std::string> &Args) {
Remove->process(Args);
});
});
}
if (!F.Add.empty()) {
std::vector<std::string> Add;
for (auto &A : F.Add)
Add.push_back(std::move(*A));
Out.Apply.push_back([Add(std::move(Add))](Config &C) {
C.CompileFlags.Edits.push_back([Add](std::vector<std::string> &Args) {
Args.insert(Args.end(), Add.begin(), Add.end());
});
});
}
}
void compile(Fragment::IndexBlock &&F) {
if (F.Background) {
if (auto Val = compileEnum<Config::BackgroundPolicy>("Background",
**F.Background)
.map("Build", Config::BackgroundPolicy::Build)
.map("Skip", Config::BackgroundPolicy::Skip)
.value())
Out.Apply.push_back([Val](Config &C) { C.Index.Background = *Val; });
}
}
constexpr static llvm::SourceMgr::DiagKind Error = llvm::SourceMgr::DK_Error;
constexpr static llvm::SourceMgr::DiagKind Warning =
llvm::SourceMgr::DK_Warning;
void diag(llvm::SourceMgr::DiagKind Kind, llvm::StringRef Message,
llvm::SMRange Range) {
if (Range.isValid() && SourceMgr != nullptr)
Diagnostic(SourceMgr->GetMessage(Range.Start, Kind, Message, Range));
else
Diagnostic(llvm::SMDiagnostic("", Kind, Message));
}
};
} // namespace
CompiledFragment Fragment::compile(DiagnosticCallback D) && {
llvm::StringRef ConfigFile = "<unknown>";
std::pair<unsigned, unsigned> LineCol = {0, 0};
if (auto *SM = Source.Manager.get()) {
unsigned BufID = SM->getMainFileID();
LineCol = SM->getLineAndColumn(Source.Location, BufID);
ConfigFile = SM->getBufferInfo(BufID).Buffer->getBufferIdentifier();
}
trace::Span Tracer("ConfigCompile");
SPAN_ATTACH(Tracer, "ConfigFile", ConfigFile);
auto Result = std::make_shared<CompiledFragmentImpl>();
vlog("Config fragment: compiling {0}:{1} -> {2}", ConfigFile, LineCol.first,
Result.get());
FragmentCompiler{*Result, D, Source.Manager.get()}.compile(std::move(*this));
// Return as cheaply-copyable wrapper.
return [Result(std::move(Result))](const Params &P, Config &C) {
return (*Result)(P, C);
};
}
} // namespace config
} // namespace clangd
} // namespace clang
|