[llvm] [IR] Add source location filtering to pass dumps (PR #203393)
Yaxun Liu via llvm-commits
llvm-commits at lists.llvm.org
Sun Aug 30 14:40:10 PDT 2026
================
@@ -163,7 +177,151 @@ bool llvm::isFilterPassesEmpty() { return FilterPasses.empty(); }
bool llvm::isFunctionInPrintList(StringRef FunctionName) {
static const StringSet<> PrintFuncNames(llvm::from_range, PrintFuncsList);
- return PrintFuncNames.empty() || PrintFuncNames.contains(FunctionName);
+ return PrintFuncNames.empty() || PrintFuncNames.contains(FunctionName) ||
+ PrintFuncNames.contains("*");
+}
+
+namespace {
+
+struct PrintLineRange {
+ unsigned First;
+ unsigned Last;
+};
+
+struct PrintSourceLocSpec {
+ std::string File;
+ SmallVector<PrintLineRange, 4> Lines;
+};
+
+[[noreturn]] void reportBadLocSpec(StringRef Spec) {
+ report_fatal_error(Twine("Invalid -filter-print-source-locs value '") + Spec +
+ "'. Expected file:line[,line-line][,line].");
+}
+
+std::string normalizeSlashes(StringRef Path) {
+ std::string Result = Path.str();
+ for (char &C : Result)
+ if (C == '\\')
+ C = '/';
+ return Result;
+}
+
+bool parseLine(StringRef LineText, unsigned &Line) {
+ return !LineText.empty() && !LineText.getAsInteger(10, Line);
+}
+
+PrintLineRange parseLineRange(StringRef RangeText, StringRef FullSpec) {
+ auto [FirstText, LastText] = RangeText.split('-');
+
+ unsigned First;
+ if (!parseLine(FirstText, First))
+ reportBadLocSpec(FullSpec);
+
+ if (LastText.empty())
+ return {First, First};
+
+ unsigned Last;
+ if (!parseLine(LastText, Last) || Last < First)
+ reportBadLocSpec(FullSpec);
+
+ return {First, Last};
+}
+
+std::vector<PrintSourceLocSpec> parseSourceLocSpecs() {
+ std::vector<PrintSourceLocSpec> Result;
+ for (const std::string &RawSpec : PrintSourceLocs) {
+ StringRef Spec(RawSpec);
+ auto [File, LineSpec] = Spec.rsplit(':');
+ if (File.empty() || LineSpec.empty())
+ reportBadLocSpec(Spec);
+
+ PrintSourceLocSpec Parsed;
+ Parsed.File = normalizeSlashes(File);
+ for (StringRef RangeText : llvm::split(LineSpec, ",")) {
+ Parsed.Lines.push_back(parseLineRange(RangeText, Spec));
+ }
+ Result.push_back(std::move(Parsed));
+ }
+ return Result;
+}
+
+ArrayRef<PrintSourceLocSpec> getSourceLocSpecs() {
+ static const std::vector<PrintSourceLocSpec> Specs = parseSourceLocSpecs();
+ return Specs;
+}
+
+std::string makeDebugLocPath(StringRef Directory, StringRef Filename) {
+ std::string NormalizedFilename = normalizeSlashes(Filename);
+ if (Directory.empty() || sys::path::is_absolute(NormalizedFilename))
+ return NormalizedFilename;
+
+ std::string NormalizedDirectory = normalizeSlashes(Directory);
+ if (NormalizedDirectory.empty())
+ return NormalizedFilename;
+ if (NormalizedDirectory.back() == '/')
+ return NormalizedDirectory + NormalizedFilename;
+ return NormalizedDirectory + "/" + NormalizedFilename;
+}
+
+bool matchesFile(StringRef SpecFile, StringRef Directory, StringRef Filename) {
----------------
yxsamliu wrote:
Added a short comment describing exact filename or path, basename, and path-suffix matching.
https://github.com/llvm/llvm-project/pull/203393
More information about the llvm-commits
mailing list