Skip to content

Commit cf8748a

Browse files
committed
[FileCheck] Improve printing variables with escapes
Firstly fix FileCheck printing string variables double-escaped (first regex, then C-style). This is confusing because it is not clear if the printed value is the literal value or exactly how it is escaped, without looking at FileCheck's source code. Secondly, only escape when doing so makes it easier to read the value (when the string contains tabs, newlines or non-printable characters). When the variable value is escaped, make a note of it in the output too, in order to avoid confusion. The common case that is motivating this change is variables that contain windows style paths with backslashes. These were printed as `"C:\\\\Program Files\\\\MyApp\\\\file.txt"`. Now prefer to print them as `"C:\Program Files\MyApp\file.txt"`. Printing the value literally also makes it easier to search for variables in the output, since the user can just copy-paste it.
1 parent 07e3c85 commit cf8748a

File tree

3 files changed

+67
-13
lines changed

3 files changed

+67
-13
lines changed

llvm/lib/FileCheck/FileCheck.cpp

Lines changed: 47 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -264,7 +264,7 @@ BinaryOperation::getImplicitFormat(const SourceMgr &SM) const {
264264
: *RightFormat;
265265
}
266266

267-
Expected<std::string> NumericSubstitution::getResult() const {
267+
Expected<std::string> NumericSubstitution::getResultRegex() const {
268268
assert(ExpressionPointer->getAST() != nullptr &&
269269
"Substituting empty expression");
270270
Expected<APInt> EvaluatedValue = ExpressionPointer->getAST()->eval();
@@ -274,14 +274,55 @@ Expected<std::string> NumericSubstitution::getResult() const {
274274
return Format.getMatchingString(*EvaluatedValue);
275275
}
276276

277-
Expected<std::string> StringSubstitution::getResult() const {
277+
Expected<std::string> NumericSubstitution::getResultForDiagnostics() const {
278+
// The "regex" returned by getResultRegex() is just a numeric value
279+
// like '42', '0x2A', '-17', 'DEADBEEF' etc. This is already suitable for use in diagnostics.
280+
Expected<std::string> Literal = getResultRegex();
281+
if (!Literal)
282+
return Literal;
283+
284+
return "\"" + std::move(*Literal) + "\"";
285+
}
286+
287+
Expected<std::string> StringSubstitution::getResultRegex() const {
278288
// Look up the value and escape it so that we can put it into the regex.
279289
Expected<StringRef> VarVal = Context->getPatternVarValue(FromStr);
280290
if (!VarVal)
281291
return VarVal.takeError();
282292
return Regex::escape(*VarVal);
283293
}
284294

295+
Expected<std::string> StringSubstitution::getResultForDiagnostics() const {
296+
Expected<StringRef> VarVal = Context->getPatternVarValue(FromStr);
297+
if (!VarVal)
298+
return VarVal.takeError();
299+
300+
std::string Result;
301+
Result.reserve(VarVal->size() + 2);
302+
raw_string_ostream OS(Result);
303+
304+
OS << '"';
305+
// Escape the string if it contains any characters that
306+
// make it hard to read, such as tabs, newlines, quotes, and non-printable characters.
307+
// Note that we do not include backslashes in this set, because they are
308+
// common in Windows paths and escaping them would make the output
309+
// harder to read.
310+
// However, when we do escape, backslashes are escaped as well,
311+
// otherwise the output would be ambiguous.
312+
const bool NeedsEscaping = llvm::any_of(*VarVal, [](char C) {
313+
return C == '\t' || C == '\n' || C == '"' || !isPrint(C);
314+
});
315+
if (NeedsEscaping)
316+
OS.write_escaped(*VarVal);
317+
else
318+
OS << *VarVal;
319+
OS << '"';
320+
if (NeedsEscaping)
321+
OS << " (escaped value)";
322+
323+
return Result;
324+
}
325+
285326
bool Pattern::isValidVarNameStart(char C) { return C == '_' || isAlpha(C); }
286327

287328
Expected<Pattern::VariableProperties>
@@ -1106,7 +1147,7 @@ Pattern::MatchResult Pattern::match(StringRef Buffer,
11061147
Error Errs = Error::success();
11071148
for (const auto &Substitution : Substitutions) {
11081149
// Substitute and check for failure (e.g. use of undefined variable).
1109-
Expected<std::string> Value = Substitution->getResult();
1150+
Expected<std::string> Value = Substitution->getResultRegex();
11101151
if (!Value) {
11111152
// Convert to an ErrorDiagnostic to get location information. This is
11121153
// done here rather than printMatch/printNoMatch since now we know which
@@ -1210,16 +1251,16 @@ void Pattern::printSubstitutions(const SourceMgr &SM, StringRef Buffer,
12101251
SmallString<256> Msg;
12111252
raw_svector_ostream OS(Msg);
12121253

1213-
Expected<std::string> MatchedValue = Substitution->getResult();
1254+
Expected<std::string> MatchedValue = Substitution->getResultForDiagnostics();
12141255
// Substitution failures are handled in printNoMatch().
12151256
if (!MatchedValue) {
12161257
consumeError(MatchedValue.takeError());
12171258
continue;
12181259
}
12191260

12201261
OS << "with \"";
1221-
OS.write_escaped(Substitution->getFromString()) << "\" equal to \"";
1222-
OS.write_escaped(*MatchedValue) << "\"";
1262+
OS.write_escaped(Substitution->getFromString()) << "\" equal to ";
1263+
OS << *MatchedValue;
12231264

12241265
// We report only the start of the match/search range to suggest we are
12251266
// reporting the substitutions as set at the start of the match/search.

llvm/lib/FileCheck/FileCheckImpl.h

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -366,9 +366,14 @@ class Substitution {
366366
/// \returns the index where the substitution is to be performed in RegExStr.
367367
size_t getIndex() const { return InsertIdx; }
368368

369-
/// \returns a string containing the result of the substitution represented
369+
/// \returns a regular expression string that matches the result of the substitution represented
370370
/// by this class instance or an error if substitution failed.
371-
virtual Expected<std::string> getResult() const = 0;
371+
virtual Expected<std::string> getResultRegex() const = 0;
372+
373+
/// \returns a string containing the result of the substitution represented
374+
/// by this class instance in a form suitable for diagnostics, or an error if
375+
/// substitution failed.
376+
virtual Expected<std::string> getResultForDiagnostics() const = 0;
372377
};
373378

374379
class StringSubstitution : public Substitution {
@@ -379,7 +384,11 @@ class StringSubstitution : public Substitution {
379384

380385
/// \returns the text that the string variable in this substitution matched
381386
/// when defined, or an error if the variable is undefined.
382-
Expected<std::string> getResult() const override;
387+
Expected<std::string> getResultRegex() const override;
388+
389+
/// \returns the text that the string variable in this substitution matched
390+
/// when defined, in a form suitable for diagnostics, or an error if the variable is undefined.
391+
Expected<std::string> getResultForDiagnostics() const override;
383392
};
384393

385394
class NumericSubstitution : public Substitution {
@@ -397,7 +406,11 @@ class NumericSubstitution : public Substitution {
397406

398407
/// \returns a string containing the result of evaluating the expression in
399408
/// this substitution, or an error if evaluation failed.
400-
Expected<std::string> getResult() const override;
409+
Expected<std::string> getResultRegex() const override;
410+
411+
/// \returns a string containing the result of evaluating the expression in
412+
/// this substitution, in a form suitable for diagnostics, or an error if evaluation failed.
413+
Expected<std::string> getResultForDiagnostics() const override;
401414
};
402415

403416
//===----------------------------------------------------------------------===//

llvm/test/FileCheck/var-escape.txt

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,9 @@ VARS-NEXT: [[WINPATH]] [[NOT_ESCAPED]] [[ESCAPED]] [[#$NUMERIC + 0]]
1414
; RUN: -dump-input=never --strict-whitespace --check-prefix=VARS --input-file=%t.1 %s 2>&1 \
1515
; RUN: | FileCheck %s
1616

17-
CHECK: with "WINPATH" equal to "A:\\\\windows\\\\style\\\\path"
18-
CHECK: with "NOT_ESCAPED" equal to "shouldn't be escaped \\[a-Z\\]\\\\\\+\\$"
19-
CHECK: with "ESCAPED" equal to "\\\\ \014\013 needs\to \"be\" escaped\\\000"
17+
CHECK: with "WINPATH" equal to "A:\windows\style\path"
18+
CHECK: with "NOT_ESCAPED" equal to "shouldn't be escaped [a-Z]\+$"
19+
CHECK: with "ESCAPED" equal to "\\ \014\013 needs\to \"be\" escaped\000" (escaped value)
2020
CHECK: with "$NUMERIC + 0" equal to "DEADBEEF"
2121

2222
; Test escaping of the name of a numeric substitution, which might contain

0 commit comments

Comments
 (0)