31 #include "llvm/ADT/STLExtras.h"
32 #include "llvm/ADT/StringRef.h"
33 #include "llvm/ADT/StringSwitch.h"
34 #include "llvm/Support/Regex.h"
35 #include "llvm/Support/SMLoc.h"
36 #include "llvm/Support/SourceMgr.h"
43 struct CompiledFragmentImpl {
49 std::vector<llvm::unique_function<bool(
const Params &)
const>>
Conditions;
52 std::vector<llvm::unique_function<void(
Config &)
const>>
Apply;
54 bool operator()(
const Params &P,
Config &C)
const {
57 dlog(
"Config fragment {0}: condition not met",
this);
61 dlog(
"Config fragment {0}: applying {1} rules",
this,
Apply.size());
62 for (
const auto &A :
Apply)
69 struct FragmentCompiler {
70 CompiledFragmentImpl &
Out;
74 llvm::Optional<llvm::Regex> compileRegex(
const Located<std::string> &Text) {
75 std::string Anchored =
"^(" + *
Text +
")$";
76 llvm::Regex Result(Anchored);
77 std::string RegexError;
78 if (!Result.isValid(RegexError)) {
79 diag(
Error,
"Invalid regex " + Anchored +
": " + RegexError,
Text.Range);
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;
94 EnumSwitch(llvm::StringRef EnumName,
const Located<std::string> &In,
95 FragmentCompiler &
Outer)
96 :
Outer(
Outer), EnumName(EnumName), Input(In) {}
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)
106 llvm::Optional<T> value() {
110 llvm::formatv(
"Invalid {0} value '{1}'. Valid values are {2}.",
111 EnumName, *Input,
llvm::join(ValidValues,
", "))
125 template <
typename T>
126 EnumSwitch<T> compileEnum(llvm::StringRef EnumName,
127 const Located<std::string> &In) {
128 return EnumSwitch<T>(EnumName, In, *
this);
131 void compile(Fragment &&F) {
132 compile(std::move(F.If));
133 compile(std::move(F.CompileFlags));
134 compile(std::move(F.Index));
137 void compile(Fragment::IfBlock &&F) {
138 if (F.HasUnrecognizedCondition)
139 Out.Conditions.push_back([&](
const Params &) {
return false; });
141 auto PathMatch = std::make_unique<std::vector<llvm::Regex>>();
142 for (
auto &
Entry : F.PathMatch) {
143 if (
auto RE = compileRegex(
Entry))
144 PathMatch->push_back(std::move(*RE));
146 if (!PathMatch->empty()) {
147 Out.Conditions.push_back(
148 [PathMatch(std::move(PathMatch))](
const Params &P) {
151 return llvm::any_of(*PathMatch, [&](
const llvm::Regex &RE) {
152 return RE.match(P.Path);
157 auto PathExclude = std::make_unique<std::vector<llvm::Regex>>();
158 for (
auto &
Entry : F.PathExclude) {
159 if (
auto RE = compileRegex(
Entry))
160 PathExclude->push_back(std::move(*RE));
162 if (!PathExclude->empty()) {
163 Out.Conditions.push_back(
164 [PathExclude(std::move(PathExclude))](
const Params &P) {
167 return llvm::none_of(*PathExclude, [&](
const llvm::Regex &RE) {
168 return RE.match(P.Path);
174 void compile(Fragment::CompileFlagsBlock &&F) {
175 if (!F.Remove.empty()) {
176 auto Remove = std::make_shared<ArgStripper>();
177 for (
auto &A : F.Remove)
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);
188 if (!F.Add.empty()) {
189 std::vector<std::string> Add;
190 for (
auto &A : F.Add)
191 Add.push_back(std::move(*A));
192 Out.Apply.push_back([Add(std::move(Add))](
Config &C) {
193 C.CompileFlags.Edits.push_back([Add](std::vector<std::string> &Args) {
194 Args.insert(Args.end(), Add.begin(), Add.end());
200 void compile(Fragment::IndexBlock &&F) {
202 if (
auto Val = compileEnum<Config::BackgroundPolicy>(
"Background",
207 Out.Apply.push_back([Val](
Config &C) { C.Index.Background = *Val; });
211 constexpr
static llvm::SourceMgr::DiagKind
Error = llvm::SourceMgr::DK_Error;
212 constexpr
static llvm::SourceMgr::DiagKind
Warning =
213 llvm::SourceMgr::DK_Warning;
214 void diag(llvm::SourceMgr::DiagKind
Kind, llvm::StringRef
Message,
215 llvm::SMRange
Range) {
226 llvm::StringRef ConfigFile =
"<unknown>";
227 std::pair<unsigned, unsigned> LineCol = {0, 0};
228 if (
auto *SM = Source.Manager.get()) {
229 unsigned BufID = SM->getMainFileID();
230 LineCol = SM->getLineAndColumn(Source.Location, BufID);
231 ConfigFile = SM->getBufferInfo(BufID).Buffer->getBufferIdentifier();
235 auto Result = std::make_shared<CompiledFragmentImpl>();
236 vlog(
"Config fragment: compiling {0}:{1} -> {2}", ConfigFile, LineCol.first,
239 FragmentCompiler{*Result, D, Source.Manager.get()}.compile(std::move(*
this));
241 return [Result(std::move(Result))](
const Params &P,
Config &C) {
242 return (*Result)(P, C);