77// ===----------------------------------------------------------------------===//
88
99#include " llvm/Analysis/DebugInfoCache.h"
10+ #include " llvm/ADT/STLExtras.h"
1011#include " llvm/AsmParser/Parser.h"
1112#include " llvm/IR/Module.h"
1213#include " llvm/IR/Verifier.h"
1718namespace llvm {
1819namespace {
1920
20- // Forward declare the assembly
21+ // Forward declare the IR string
2122extern StringRef MultiCUModule;
2223
23- const DICompileUnit *findCU (const Module &M, StringRef FileName) {
24- for (const auto CU : M.debug_compile_units ()) {
25- if (CU->getFilename () == FileName)
26- return CU;
27- }
24+ DICompileUnit *findCU (const Module &M, StringRef FileName) {
25+ auto CUs = M.debug_compile_units ();
26+ auto Matching = llvm::find_if (
27+ CUs, [&](auto *CU) { return CU->getFilename () == FileName; });
28+ return Matching != CUs.end () ? *Matching : nullptr ;
29+ }
30+
31+ void checkEqualDI (const DebugInfoFinder &DIFinder1,
32+ const DebugInfoFinder &DIFinder2) {
33+ EXPECT_TRUE (
34+ llvm::equal (DIFinder1.compile_units (), DIFinder2.compile_units ()));
35+ EXPECT_TRUE (llvm::equal (DIFinder1.types (), DIFinder2.types ()));
36+ EXPECT_TRUE (llvm::equal (DIFinder1.subprograms (), DIFinder2.subprograms ()));
37+ EXPECT_TRUE (llvm::equal (DIFinder1.scopes (), DIFinder2.scopes ()));
38+ }
39+
40+ void checkCachedDISameAsFromScratch (llvm::Module &M, const DebugInfoCache &DIC,
41+ StringRef CUName) {
42+ auto *CU = findCU (M, CUName);
43+ EXPECT_NE (CU, nullptr );
44+
45+ auto CachedDIFinder = DIC.Result .find (CU);
46+ EXPECT_NE (CachedDIFinder, DIC.Result .end ());
2847
29- return nullptr ;
48+ DebugInfoFinder ExpectedDIFinder;
49+ ExpectedDIFinder.processCompileUnit (CU);
50+
51+ checkEqualDI (CachedDIFinder->getSecond (), ExpectedDIFinder);
3052}
3153
3254class DebugInfoCacheTest : public testing ::Test {
3355protected:
3456 LLVMContext C;
3557
36- std::unique_ptr<Module> makeModule (StringRef Assembly ) {
58+ std::unique_ptr<Module> makeModule (StringRef IR ) {
3759 SMDiagnostic Err;
38- auto M = parseAssemblyString (Assembly , Err, C);
60+ auto M = parseAssemblyString (IR , Err, C);
3961 if (!M)
4062 Err.print (" DebugInfoCacheTest" , errs ());
4163
@@ -55,27 +77,8 @@ TEST_F(DebugInfoCacheTest, TestMultiCU) {
5577 DebugInfoCache DIC{*M};
5678 EXPECT_EQ (DIC.Result .size (), 2u );
5779
58- auto *File1CU = findCU (*M, " file1.cpp" );
59- EXPECT_NE (File1CU, nullptr );
60-
61- auto File1DIFinder = DIC.Result .find (File1CU);
62- EXPECT_NE (File1DIFinder, DIC.Result .end ());
63-
64- EXPECT_EQ (File1DIFinder->getSecond ().compile_unit_count (), 1u );
65- EXPECT_EQ (File1DIFinder->getSecond ().type_count (), 6u );
66- EXPECT_EQ (File1DIFinder->getSecond ().subprogram_count (), 0u );
67- EXPECT_EQ (File1DIFinder->getSecond ().scope_count (), 1u );
68-
69- auto *File2CU = findCU (*M, " file2.cpp" );
70- EXPECT_NE (File1CU, nullptr );
71-
72- auto File2DIFinder = DIC.Result .find (File2CU);
73- EXPECT_NE (File2DIFinder, DIC.Result .end ());
74-
75- EXPECT_EQ (File2DIFinder->getSecond ().compile_unit_count (), 1u );
76- EXPECT_EQ (File2DIFinder->getSecond ().type_count (), 2u );
77- EXPECT_EQ (File2DIFinder->getSecond ().subprogram_count (), 0u );
78- EXPECT_EQ (File2DIFinder->getSecond ().scope_count (), 2u );
80+ checkCachedDISameAsFromScratch (*M, DIC, " file1.cpp" );
81+ checkCachedDISameAsFromScratch (*M, DIC, " file2.cpp" );
7982}
8083
8184/* Generated roughly by
0 commit comments