|
36 | 36 | #include "llvm/Support/Debug.h"
|
37 | 37 | #include "llvm/Support/FileSystem.h"
|
38 | 38 | #include "llvm/Support/Path.h"
|
| 39 | +#include "llvm/Support/Regex.h" |
39 | 40 | #include "llvm/Support/raw_ostream.h"
|
40 | 41 | #include "llvm/Transforms/Instrumentation.h"
|
41 | 42 | #include "llvm/Transforms/Instrumentation/GCOVProfiler.h"
|
@@ -96,6 +97,11 @@ class GCOVProfiler {
|
96 | 97 | // profiling runtime to emit .gcda files when run.
|
97 | 98 | bool emitProfileArcs();
|
98 | 99 |
|
| 100 | + bool isFunctionInstrumented(const Function &F); |
| 101 | + std::vector<Regex> createRegexesFromString(StringRef RegexesStr); |
| 102 | + static bool doesFilenameMatchARegex(StringRef Filename, |
| 103 | + std::vector<Regex> &Regexes); |
| 104 | + |
99 | 105 | // Get pointers to the functions in the runtime library.
|
100 | 106 | Constant *getStartFileFunc();
|
101 | 107 | Constant *getEmitFunctionFunc();
|
@@ -125,6 +131,9 @@ class GCOVProfiler {
|
125 | 131 | const TargetLibraryInfo *TLI;
|
126 | 132 | LLVMContext *Ctx;
|
127 | 133 | SmallVector<std::unique_ptr<GCOVFunction>, 16> Funcs;
|
| 134 | + std::vector<Regex> FilterRe; |
| 135 | + std::vector<Regex> ExcludeRe; |
| 136 | + StringMap<bool> InstrumentedFiles; |
128 | 137 | };
|
129 | 138 |
|
130 | 139 | class GCOVProfilerLegacyPass : public ModulePass {
|
@@ -423,6 +432,72 @@ namespace {
|
423 | 432 | };
|
424 | 433 | }
|
425 | 434 |
|
| 435 | +// RegexesStr is a string containing differents regex separated by a semi-colon. |
| 436 | +// For example "foo\..*$;bar\..*$". |
| 437 | +std::vector<Regex> GCOVProfiler::createRegexesFromString(StringRef RegexesStr) { |
| 438 | + std::vector<Regex> Regexes; |
| 439 | + while (!RegexesStr.empty()) { |
| 440 | + std::pair<StringRef, StringRef> HeadTail = RegexesStr.split(';'); |
| 441 | + if (!HeadTail.first.empty()) { |
| 442 | + Regex Re(HeadTail.first); |
| 443 | + std::string Err; |
| 444 | + if (!Re.isValid(Err)) { |
| 445 | + Ctx->emitError(Twine("Regex ") + HeadTail.first + |
| 446 | + " is not valid: " + Err); |
| 447 | + } |
| 448 | + Regexes.emplace_back(std::move(Re)); |
| 449 | + } |
| 450 | + RegexesStr = HeadTail.second; |
| 451 | + } |
| 452 | + return Regexes; |
| 453 | +} |
| 454 | + |
| 455 | +bool GCOVProfiler::doesFilenameMatchARegex(StringRef Filename, |
| 456 | + std::vector<Regex> &Regexes) { |
| 457 | + for (Regex &Re : Regexes) { |
| 458 | + if (Re.match(Filename)) { |
| 459 | + return true; |
| 460 | + } |
| 461 | + } |
| 462 | + return false; |
| 463 | +} |
| 464 | + |
| 465 | +bool GCOVProfiler::isFunctionInstrumented(const Function &F) { |
| 466 | + if (FilterRe.empty() && ExcludeRe.empty()) { |
| 467 | + return true; |
| 468 | + } |
| 469 | + const StringRef Filename = F.getSubprogram()->getFilename(); |
| 470 | + auto It = InstrumentedFiles.find(Filename); |
| 471 | + if (It != InstrumentedFiles.end()) { |
| 472 | + return It->second; |
| 473 | + } |
| 474 | + |
| 475 | + SmallString<256> RealPath; |
| 476 | + StringRef RealFilename; |
| 477 | + |
| 478 | + // Path can be |
| 479 | + // /usr/lib/gcc/x86_64-linux-gnu/8/../../../../include/c++/8/bits/*.h so for |
| 480 | + // such a case we must get the real_path. |
| 481 | + if (sys::fs::real_path(Filename, RealPath)) { |
| 482 | + // real_path can fail with path like "foo.c". |
| 483 | + RealFilename = Filename; |
| 484 | + } else { |
| 485 | + RealFilename = RealPath; |
| 486 | + } |
| 487 | + |
| 488 | + bool ShouldInstrument; |
| 489 | + if (FilterRe.empty()) { |
| 490 | + ShouldInstrument = !doesFilenameMatchARegex(RealFilename, ExcludeRe); |
| 491 | + } else if (ExcludeRe.empty()) { |
| 492 | + ShouldInstrument = doesFilenameMatchARegex(RealFilename, FilterRe); |
| 493 | + } else { |
| 494 | + ShouldInstrument = doesFilenameMatchARegex(RealFilename, FilterRe) && |
| 495 | + !doesFilenameMatchARegex(RealFilename, ExcludeRe); |
| 496 | + } |
| 497 | + InstrumentedFiles[Filename] = ShouldInstrument; |
| 498 | + return ShouldInstrument; |
| 499 | +} |
| 500 | + |
426 | 501 | std::string GCOVProfiler::mangleName(const DICompileUnit *CU,
|
427 | 502 | GCovFileType OutputType) {
|
428 | 503 | bool Notes = OutputType == GCovFileType::GCNO;
|
@@ -472,6 +547,9 @@ bool GCOVProfiler::runOnModule(Module &M, const TargetLibraryInfo &TLI) {
|
472 | 547 |
|
473 | 548 | AddFlushBeforeForkAndExec();
|
474 | 549 |
|
| 550 | + FilterRe = createRegexesFromString(Options.Filter); |
| 551 | + ExcludeRe = createRegexesFromString(Options.Exclude); |
| 552 | + |
475 | 553 | if (Options.EmitNotes) emitProfileNotes();
|
476 | 554 | if (Options.EmitData) return emitProfileArcs();
|
477 | 555 | return false;
|
@@ -589,7 +667,8 @@ void GCOVProfiler::emitProfileNotes() {
|
589 | 667 | for (auto &F : M->functions()) {
|
590 | 668 | DISubprogram *SP = F.getSubprogram();
|
591 | 669 | if (!SP) continue;
|
592 |
| - if (!functionHasLines(F)) continue; |
| 670 | + if (!functionHasLines(F) || !isFunctionInstrumented(F)) |
| 671 | + continue; |
593 | 672 | // TODO: Functions using scope-based EH are currently not supported.
|
594 | 673 | if (isUsingScopeBasedEH(F)) continue;
|
595 | 674 |
|
@@ -673,7 +752,8 @@ bool GCOVProfiler::emitProfileArcs() {
|
673 | 752 | for (auto &F : M->functions()) {
|
674 | 753 | DISubprogram *SP = F.getSubprogram();
|
675 | 754 | if (!SP) continue;
|
676 |
| - if (!functionHasLines(F)) continue; |
| 755 | + if (!functionHasLines(F) || !isFunctionInstrumented(F)) |
| 756 | + continue; |
677 | 757 | // TODO: Functions using scope-based EH are currently not supported.
|
678 | 758 | if (isUsingScopeBasedEH(F)) continue;
|
679 | 759 | if (!Result) Result = true;
|
|
0 commit comments