llvm.org GIT mirror llvm / 66a92f3
Reapply "[Coverage] Prevent detection of false instantiations in case of macro expansion." The root of the problem was that findMainViewFileID(File, Function) could return some ID for any given file, even though that file was not the main file for that function. This patch ensures that the result of this function is conformed with the result of findMainViewFileID(Function). This commit reapplies r266436, which was reverted by r266458, with the .covmapping file serialized in v1 format. Differential Revision: http://reviews.llvm.org/D18787 git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@266620 91177308-0d34-0410-b5e6-96231b3b80d8 Igor Kudrin 4 years ago
7 changed file(s) with 102 addition(s) and 19 deletion(s). Raw diff Collapse all Expand all
374374 return FilenameEquivalence;
375375 }
376376
377 static Optional findMainViewFileID(StringRef SourceFile,
378 const FunctionRecord &Function) {
379 SmallBitVector IsNotExpandedFile(Function.Filenames.size(), true);
380 SmallBitVector FilenameEquivalence = gatherFileIDs(SourceFile, Function);
381 for (const auto &CR : Function.CountedRegions)
382 if (CR.Kind == CounterMappingRegion::ExpansionRegion &&
383 FilenameEquivalence[CR.FileID])
384 IsNotExpandedFile[CR.ExpandedFileID] = false;
385 IsNotExpandedFile &= FilenameEquivalence;
386 int I = IsNotExpandedFile.find_first();
387 if (I == -1)
388 return None;
389 return I;
390 }
391
377 /// Return the ID of the file where the definition of the function is located.
392378 static Optional findMainViewFileID(const FunctionRecord &Function) {
393379 SmallBitVector IsNotExpandedFile(Function.Filenames.size(), true);
394380 for (const auto &CR : Function.CountedRegions)
400386 return I;
401387 }
402388
389 /// Check if SourceFile is the file that contains the definition of
390 /// the Function. Return the ID of the file in that case or None otherwise.
391 static Optional findMainViewFileID(StringRef SourceFile,
392 const FunctionRecord &Function) {
393 Optional I = findMainViewFileID(Function);
394 if (I && SourceFile == Function.Filenames[*I])
395 return I;
396 return None;
397 }
398
403399 /// Sort a nested sequence of regions from a single file.
404400 template static void sortNestedRegions(It First, It Last) {
405401 std::sort(First, Last,
421417
422418 for (const auto &Function : Functions) {
423419 auto MainFileID = findMainViewFileID(Filename, Function);
424 if (!MainFileID)
425 continue;
426420 auto FileIDs = gatherFileIDs(Filename, Function);
427421 for (const auto &CR : Function.CountedRegions)
428422 if (FileIDs.test(CR.FileID)) {
429423 Regions.push_back(CR);
430 if (isExpansion(CR, *MainFileID))
424 if (MainFileID && isExpansion(CR, *MainFileID))
431425 FileCoverage.Expansions.emplace_back(CR, Function);
432426 }
433427 }
0 #include "prevent_false_instantiations.h"
1
2 void func1() {
3 DO_SOMETHING();
4 }
5
6 void func2() {
7 DO_SOMETHING();
8 }
9
10 int main() {
11 func1();
12 func2();
13 return 0;
14 }
0 _Z5func1v
1 # Func Hash:
2 3
3 # Num Counters:
4 2
5 # Counter Values:
6 1
7 0
8
9 _Z5func2v
10 # Func Hash:
11 3
12 # Num Counters:
13 2
14 # Counter Values:
15 1
16 0
17
18 main
19 # Func Hash:
20 0
21 # Num Counters:
22 1
23 # Counter Values:
24 1
25
None config.suffixes = ['.test', '.m', '.cpp', '.c']
0 config.suffixes = ['.test', '.m', '.cpp', '.c', '.h']
0 // Checks that function instantiations don't go to a wrong file.
1
2 // CHECK-NOT: {{_Z5func[1,2]v}}
3
4 // RUN: llvm-profdata merge %S/Inputs/prevent_false_instantiations.proftext -o %t.profdata
5 // RUN: llvm-cov show %S/Inputs/prevent_false_instantiations.covmapping -instr-profile %t.profdata -filename-equivalence %s | FileCheck %s
6
7 #define DO_SOMETHING() \
8 do { \
9 } while (0)
453453 ASSERT_EQ("func", Names[0]);
454454 }
455455
456 TEST_P(MaybeSparseCoverageMappingTest, dont_detect_false_instantiations) {
457 InstrProfRecord Record1("foo", 0x1234, {10});
458 InstrProfRecord Record2("bar", 0x2345, {20});
459 ProfileWriter.addRecord(std::move(Record1));
460 ProfileWriter.addRecord(std::move(Record2));
461
462 startFunction("foo", 0x1234);
463 addCMR(Counter::getCounter(0), "expanded", 1, 1, 1, 10);
464 addExpansionCMR("main", "expanded", 4, 1, 4, 5);
465
466 startFunction("bar", 0x2345);
467 addCMR(Counter::getCounter(0), "expanded", 1, 1, 1, 10);
468 addExpansionCMR("main", "expanded", 9, 1, 9, 5);
469
470 loadCoverageMapping();
471
472 std::vector Instantiations =
473 LoadedCoverage->getInstantiations("expanded");
474 ASSERT_TRUE(Instantiations.empty());
475 }
476
477 TEST_P(MaybeSparseCoverageMappingTest, load_coverage_for_expanded_file) {
478 InstrProfRecord Record("func", 0x1234, {10});
479 ProfileWriter.addRecord(std::move(Record));
480
481 startFunction("func", 0x1234);
482 addCMR(Counter::getCounter(0), "expanded", 1, 1, 1, 10);
483 addExpansionCMR("main", "expanded", 4, 1, 4, 5);
484
485 loadCoverageMapping();
486
487 CoverageData Data = LoadedCoverage->getCoverageForFile("expanded");
488 std::vector Segments(Data.begin(), Data.end());
489 ASSERT_EQ(2U, Segments.size());
490 EXPECT_EQ(CoverageSegment(1, 1, 10, true), Segments[0]);
491 EXPECT_EQ(CoverageSegment(1, 10, false), Segments[1]);
492 }
493
456494 INSTANTIATE_TEST_CASE_P(MaybeSparse, MaybeSparseCoverageMappingTest,
457495 ::testing::Bool());
458496