llvm.org GIT mirror llvm / 59c0c36
Prevent DILocation::getMergedLocation() from creating invalid metadata. The function's new implementation from r340583 had a bug in it that could cause an invalid scope to be generated when merging two DILocations with no common ancestor scope. This patch detects this situation and picks the scope of the first location. This is not perfect, because the scope is misleading, but on the other hand, this will be a line 0 location. rdar://problem/43687474 Differential Revision: https://reviews.llvm.org/D51238 git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@340672 91177308-0d34-0410-b5e6-96231b3b80d8 Adrian Prantl 2 years ago
2 changed file(s) with 83 addition(s) and 0 deletion(s). Raw diff Collapse all Expand all
101101 L = L->getInlinedAt();
102102 }
103103 }
104
105 // If the two locations are irreconsilable, just pick one. This is misleading,
106 // but on the other hand, it's a "line 0" location.
107 if (!S || !isa(S))
108 S = LocA->getScope();
104109 return DILocation::get(Result->getContext(), 0, 0, S, L);
105110 }
106111
856856 DILocation *L = DILocation::get(Context, UINT32_MAX, U16 + 1, N);
857857 EXPECT_EQ(UINT32_MAX, L->getLine());
858858 EXPECT_EQ(0u, L->getColumn());
859 }
860 }
861
862 TEST_F(DILocationTest, Merge) {
863 DISubprogram *N = getSubprogram();
864 DIScope *S = DILexicalBlock::get(Context, N, getFile(), 3, 4);
865
866 {
867 // Identical.
868 auto *A = DILocation::get(Context, 2, 7, N);
869 auto *B = DILocation::get(Context, 2, 7, N);
870 auto *M = DILocation::getMergedLocation(A, B);
871 EXPECT_EQ(2u, M->getLine());
872 EXPECT_EQ(7u, M->getColumn());
873 EXPECT_EQ(N, M->getScope());
874 }
875
876 {
877 // Identical, different scopes.
878 auto *A = DILocation::get(Context, 2, 7, N);
879 auto *B = DILocation::get(Context, 2, 7, S);
880 auto *M = DILocation::getMergedLocation(A, B);
881 EXPECT_EQ(0u, M->getLine()); // FIXME: Should this be 2?
882 EXPECT_EQ(0u, M->getColumn()); // FIXME: Should this be 7?
883 EXPECT_EQ(N, M->getScope());
884 }
885
886 {
887 // Different lines, same scopes.
888 auto *A = DILocation::get(Context, 1, 6, N);
889 auto *B = DILocation::get(Context, 2, 7, N);
890 auto *M = DILocation::getMergedLocation(A, B);
891 EXPECT_EQ(0u, M->getLine());
892 EXPECT_EQ(0u, M->getColumn());
893 EXPECT_EQ(N, M->getScope());
894 }
895
896 {
897 // Twisty locations, all different, same function.
898 auto *A = DILocation::get(Context, 1, 6, N);
899 auto *B = DILocation::get(Context, 2, 7, S);
900 auto *M = DILocation::getMergedLocation(A, B);
901 EXPECT_EQ(0u, M->getLine());
902 EXPECT_EQ(0u, M->getColumn());
903 EXPECT_EQ(N, M->getScope());
904 }
905
906 {
907 // Different function, same inlined-at.
908 auto *F = getFile();
909 auto *SP1 = DISubprogram::getDistinct(Context, F, "a", "a", F, 0, nullptr,
910 false, false, 0, nullptr, 0, 0, 0,
911 DINode::FlagZero, false, nullptr);
912 auto *SP2 = DISubprogram::getDistinct(Context, F, "b", "b", F, 0, nullptr,
913 false, false, 0, nullptr, 0, 0, 0,
914 DINode::FlagZero, false, nullptr);
915
916 auto *I = DILocation::get(Context, 2, 7, N);
917 auto *A = DILocation::get(Context, 1, 6, SP1, I);
918 auto *B = DILocation::get(Context, 2, 7, SP2, I);
919 auto *M = DILocation::getMergedLocation(A, B);
920 EXPECT_EQ(0u, M->getLine());
921 EXPECT_EQ(0u, M->getColumn());
922 EXPECT_TRUE(isa(M->getScope()));
923 EXPECT_EQ(I, M->getInlinedAt());
924 }
925
926 {
927 // Completely different.
928 auto *I = DILocation::get(Context, 2, 7, N);
929 auto *A = DILocation::get(Context, 1, 6, S, I);
930 auto *B = DILocation::get(Context, 2, 7, getSubprogram());
931 auto *M = DILocation::getMergedLocation(A, B);
932 EXPECT_EQ(0u, M->getLine());
933 EXPECT_EQ(0u, M->getColumn());
934 EXPECT_TRUE(isa(M->getScope()));
935 EXPECT_EQ(S, M->getScope());
936 EXPECT_EQ(nullptr, M->getInlinedAt());
859937 }
860938 }
861939