@@ -883,6 +883,81 @@ TEST_F(DILocationTest, cloneTemporary) {
883
883
EXPECT_TRUE (L2->isTemporary ());
884
884
}
885
885
886
+ TEST_F (DILocationTest, getMergedLocation) {
887
+ FunctionType *FT = FunctionType::get (Type::getVoidTy (Context), false );
888
+ auto *F = Function::Create (FT, GlobalValue::ExternalLinkage);
889
+ const CallInst *Call = CallInst::Create (F, None);
890
+
891
+ // Merging two identical locations should result in the same location.
892
+ {
893
+ DISubprogram *Prg = getSubprogram ();
894
+ DILocation *L1 = DILocation::get (Context, 2 , 7 , Prg);
895
+ DILocation *L2 = DILocation::get (Context, 2 , 7 , Prg);
896
+ const DILocation *L = DILocation::getMergedLocation (L1, L2, nullptr );
897
+ EXPECT_EQ (2u , L->getLine ());
898
+ EXPECT_EQ (7u , L->getColumn ());
899
+ EXPECT_EQ (Prg, L->getScope ());
900
+ }
901
+ // If the two locations are incompatible, there should be a null result.
902
+ {
903
+ DISubprogram *Prg = getSubprogram ();
904
+ DILocation *L1 = DILocation::get (Context, 2 , 7 , Prg);
905
+ DILocation *L2 = DILocation::get (Context, 3 , 7 , Prg);
906
+ const DILocation *L = DILocation::getMergedLocation (L1, L2, nullptr );
907
+ EXPECT_EQ (nullptr , L);
908
+ }
909
+ // Unless we are merging locations for a call instruction. If the
910
+ // scopes agree, the result should be a compiler-generated location
911
+ // in that same scope.
912
+ {
913
+ DISubprogram *Prg = getSubprogram ();
914
+ DILocation *L1 = DILocation::get (Context, 2 , 7 , Prg);
915
+ DILocation *L2 = DILocation::get (Context, 3 , 7 , Prg);
916
+ const DILocation *L = DILocation::getMergedLocation (L1, L2, Call);
917
+ EXPECT_EQ (0u , L->getLine ());
918
+ EXPECT_EQ (Prg, L->getScope ());
919
+ }
920
+ // Likewise if the one scope is a sub-scope of the other.
921
+ {
922
+ DISubprogram *Prg = getSubprogram ();
923
+ DILexicalBlock *B = DILexicalBlock::get (Context, Prg, getFile (), 5 , 7 );
924
+ DILocation *L1 = DILocation::get (Context, 2 , 7 , Prg);
925
+ DILocation *L2 = DILocation::get (Context, 3 , 7 , B);
926
+ const DILocation *L = DILocation::getMergedLocation (L1, L2, Call);
927
+ EXPECT_EQ (0u , L->getLine ());
928
+ EXPECT_EQ (Prg, L->getScope ());
929
+ }
930
+ // Even if the calls were inlined (possibly into different places),
931
+ // if they originate from the same scope, the result should also be
932
+ // a compiler-generated location in that same scope.
933
+ {
934
+ DISubprogram *IPrg = getSubprogram ();
935
+ DILocation *I1 = DILocation::get (Context, 10 , 7 , IPrg);
936
+ DILocation *I2 = DILocation::get (Context, 11 , 7 , IPrg);
937
+ DISubprogram *Prg = getSubprogram ();
938
+ DILocation *L1 = DILocation::get (Context, 2 , 7 , Prg, I1);
939
+ DILocation *L2 = DILocation::get (Context, 3 , 7 , Prg, I2);
940
+ const DILocation *L = DILocation::getMergedLocation (L1, L2, Call);
941
+ EXPECT_EQ (0u , L->getLine ());
942
+ EXPECT_EQ (Prg, L->getScope ());
943
+ EXPECT_NE (nullptr , L->getInlinedAt ());
944
+ }
945
+ // Likewise if the one scope is a sub-scope of the other.
946
+ {
947
+ DISubprogram *IPrg = getSubprogram ();
948
+ DILocation *I1 = DILocation::get (Context, 10 , 7 , IPrg);
949
+ DILocation *I2 = DILocation::get (Context, 11 , 7 , IPrg);
950
+ DISubprogram *Prg = getSubprogram ();
951
+ DILexicalBlock *B = DILexicalBlock::get (Context, Prg, getFile (), 5 , 7 );
952
+ DILocation *L1 = DILocation::get (Context, 2 , 7 , Prg, I1);
953
+ DILocation *L2 = DILocation::get (Context, 3 , 7 , B, I2);
954
+ const DILocation *L = DILocation::getMergedLocation (L1, L2, Call);
955
+ EXPECT_EQ (0u , L->getLine ());
956
+ EXPECT_EQ (Prg, L->getScope ());
957
+ EXPECT_NE (nullptr , L->getInlinedAt ());
958
+ }
959
+ }
960
+
886
961
typedef MetadataTest GenericDINodeTest;
887
962
888
963
TEST_F (GenericDINodeTest, get) {
0 commit comments