Skip to content

Commit

Permalink
[clang][codegen] Remember string used to create llvm::Regex for optim…
Browse files Browse the repository at this point in the history
…ization remarks

Regular expression patterns passed through the command line are being used to create an instances of `llvm::Regex` and thrown away.

There is no API to serialize `Regex` back to the original pattern. This means we have no way to reconstruct the original pattern from command line. This is necessary for serializing `CompilerInvocation`.

This patch stores the original pattern string in `CodeGenOptions` alongside the `llvm::Regex` instance.

Reviewed By: dexonsmith, thegameg

Differential Revision: https://reviews.llvm.org/D96036
  • Loading branch information
jansvoboda11 committed Feb 9, 2021
1 parent d7d0b17 commit ec12f5f
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 6 deletions.
16 changes: 13 additions & 3 deletions clang/include/clang/Basic/CodeGenOptions.h
Original file line number Diff line number Diff line change
Expand Up @@ -278,27 +278,37 @@ class CodeGenOptions : public CodeGenOptionsBase {
/// -fsymbol-partition (see https://lld.llvm.org/Partitions.html).
std::string SymbolPartition;

/// Regular expression and the string it was created from.
struct RemarkPattern {
std::string Pattern;
std::shared_ptr<llvm::Regex> Regex;

explicit operator bool() const { return Regex != nullptr; }

llvm::Regex *operator->() const { return Regex.get(); }
};

/// Regular expression to select optimizations for which we should enable
/// optimization remarks. Transformation passes whose name matches this
/// expression (and support this feature), will emit a diagnostic
/// whenever they perform a transformation. This is enabled by the
/// -Rpass=regexp flag.
std::shared_ptr<llvm::Regex> OptimizationRemarkPattern;
RemarkPattern OptimizationRemarkPattern;

/// Regular expression to select optimizations for which we should enable
/// missed optimization remarks. Transformation passes whose name matches this
/// expression (and support this feature), will emit a diagnostic
/// whenever they tried but failed to perform a transformation. This is
/// enabled by the -Rpass-missed=regexp flag.
std::shared_ptr<llvm::Regex> OptimizationRemarkMissedPattern;
RemarkPattern OptimizationRemarkMissedPattern;

/// Regular expression to select optimizations for which we should enable
/// optimization analyses. Transformation passes whose name matches this
/// expression (and support this feature), will emit a diagnostic
/// whenever they want to explain why they decided to apply or not apply
/// a given transformation. This is enabled by the -Rpass-analysis=regexp
/// flag.
std::shared_ptr<llvm::Regex> OptimizationRemarkAnalysisPattern;
RemarkPattern OptimizationRemarkAnalysisPattern;

/// Set of files defining the rules for the symbol rewriting.
std::vector<std::string> RewriteMapFiles;
Expand Down
6 changes: 3 additions & 3 deletions clang/lib/Frontend/CompilerInvocation.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1164,8 +1164,8 @@ static void parseAnalyzerConfigs(AnalyzerOptions &AnOpts,
}

/// Create a new Regex instance out of the string value in \p RpassArg.
/// It returns a pointer to the newly generated Regex instance.
static std::shared_ptr<llvm::Regex>
/// It returns the string and a pointer to the newly generated Regex instance.
static CodeGenOptions::RemarkPattern
GenerateOptimizationRemarkRegex(DiagnosticsEngine &Diags, ArgList &Args,
Arg *RpassArg) {
StringRef Val = RpassArg->getValue();
Expand All @@ -1176,7 +1176,7 @@ GenerateOptimizationRemarkRegex(DiagnosticsEngine &Diags, ArgList &Args,
<< RegexError << RpassArg->getAsString(Args);
Pattern.reset();
}
return Pattern;
return {std::string(Val), Pattern};
}

static bool parseDiagnosticLevelMask(StringRef FlagName,
Expand Down

0 comments on commit ec12f5f

Please sign in to comment.