Skip to content

Commit a9f10eb

Browse files
committed
[ASTImporter] Various source location and range import fixes.
Summary: ASTImporter contained wrong or missing imports of SourceLocation and SourceRange for some objects. At least a part of such errors is fixed now. Source location import fixes in namespace, enum, record, class template specialization declarations and DeclRefExpr, UnresolvedLookupExpr, UnresolvedMemberExpr, NestedNameSpecifierLoc. Reviewers: martong, a.sidorin, shafik Reviewed By: shafik Subscribers: rnkovacs, dkrupp, Szelethus, gamesh411, cfe-commits Tags: #clang Differential Revision: https://reviews.llvm.org/D60499
1 parent 5c517a6 commit a9f10eb

File tree

7 files changed

+64
-20
lines changed

7 files changed

+64
-20
lines changed

clang/lib/AST/ASTImporter.cpp

Lines changed: 39 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -2228,6 +2228,9 @@ ExpectedDecl ASTNodeImporter::VisitNamespaceDecl(NamespaceDecl *D) {
22282228
ExpectedSLoc BeginLocOrErr = import(D->getBeginLoc());
22292229
if (!BeginLocOrErr)
22302230
return BeginLocOrErr.takeError();
2231+
ExpectedSLoc RBraceLocOrErr = import(D->getRBraceLoc());
2232+
if (!RBraceLocOrErr)
2233+
return RBraceLocOrErr.takeError();
22312234

22322235
// Create the "to" namespace, if needed.
22332236
NamespaceDecl *ToNamespace = MergeWithNamespace;
@@ -2237,6 +2240,7 @@ ExpectedDecl ASTNodeImporter::VisitNamespaceDecl(NamespaceDecl *D) {
22372240
*BeginLocOrErr, Loc, Name.getAsIdentifierInfo(),
22382241
/*PrevDecl=*/nullptr))
22392242
return ToNamespace;
2243+
ToNamespace->setRBraceLoc(*RBraceLocOrErr);
22402244
ToNamespace->setLexicalDeclContext(LexicalDC);
22412245
LexicalDC->addDeclInternal(ToNamespace);
22422246

@@ -2545,9 +2549,10 @@ ExpectedDecl ASTNodeImporter::VisitEnumDecl(EnumDecl *D) {
25452549
SourceLocation ToBeginLoc;
25462550
NestedNameSpecifierLoc ToQualifierLoc;
25472551
QualType ToIntegerType;
2548-
if (auto Imp = importSeq(
2549-
D->getBeginLoc(), D->getQualifierLoc(), D->getIntegerType()))
2550-
std::tie(ToBeginLoc, ToQualifierLoc, ToIntegerType) = *Imp;
2552+
SourceRange ToBraceRange;
2553+
if (auto Imp = importSeq(D->getBeginLoc(), D->getQualifierLoc(),
2554+
D->getIntegerType(), D->getBraceRange()))
2555+
std::tie(ToBeginLoc, ToQualifierLoc, ToIntegerType, ToBraceRange) = *Imp;
25512556
else
25522557
return Imp.takeError();
25532558

@@ -2561,6 +2566,7 @@ ExpectedDecl ASTNodeImporter::VisitEnumDecl(EnumDecl *D) {
25612566

25622567
D2->setQualifierInfo(ToQualifierLoc);
25632568
D2->setIntegerType(ToIntegerType);
2569+
D2->setBraceRange(ToBraceRange);
25642570
D2->setAccess(D->getAccess());
25652571
D2->setLexicalDeclContext(LexicalDC);
25662572
LexicalDC->addDeclInternal(D2);
@@ -2795,6 +2801,10 @@ ExpectedDecl ASTNodeImporter::VisitRecordDecl(RecordDecl *D) {
27952801
LexicalDC->addDeclInternal(D2);
27962802
}
27972803

2804+
if (auto BraceRangeOrErr = import(D->getBraceRange()))
2805+
D2->setBraceRange(*BraceRangeOrErr);
2806+
else
2807+
return BraceRangeOrErr.takeError();
27982808
if (auto QualifierLocOrErr = import(D->getQualifierLoc()))
27992809
D2->setQualifierInfo(*QualifierLocOrErr);
28002810
else
@@ -5295,6 +5305,11 @@ ExpectedDecl ASTNodeImporter::VisitClassTemplateSpecializationDecl(
52955305
LexicalDC->addDeclInternal(D2);
52965306
}
52975307

5308+
if (auto BraceRangeOrErr = import(D->getBraceRange()))
5309+
D2->setBraceRange(*BraceRangeOrErr);
5310+
else
5311+
return BraceRangeOrErr.takeError();
5312+
52985313
// Import the qualifier, if any.
52995314
if (auto LocOrErr = import(D->getQualifierLoc()))
53005315
D2->setQualifierInfo(*LocOrErr);
@@ -6293,7 +6308,8 @@ ExpectedStmt ASTNodeImporter::VisitDeclRefExpr(DeclRefExpr *E) {
62936308
TemplateArgumentListInfo *ToResInfo = nullptr;
62946309
if (E->hasExplicitTemplateArgs()) {
62956310
if (Error Err =
6296-
ImportTemplateArgumentListInfo(E->template_arguments(), ToTAInfo))
6311+
ImportTemplateArgumentListInfo(E->getLAngleLoc(), E->getRAngleLoc(),
6312+
E->template_arguments(), ToTAInfo))
62976313
return std::move(Err);
62986314
ToResInfo = &ToTAInfo;
62996315
}
@@ -7369,20 +7385,19 @@ ExpectedStmt ASTNodeImporter::VisitCXXDependentScopeMemberExpr(
73697385

73707386
ExpectedStmt
73717387
ASTNodeImporter::VisitDependentScopeDeclRefExpr(DependentScopeDeclRefExpr *E) {
7372-
auto Imp = importSeq(
7373-
E->getQualifierLoc(), E->getTemplateKeywordLoc(), E->getDeclName(),
7374-
E->getExprLoc(), E->getLAngleLoc(), E->getRAngleLoc());
7388+
auto Imp = importSeq(E->getQualifierLoc(), E->getTemplateKeywordLoc(),
7389+
E->getDeclName(), E->getNameInfo().getLoc(),
7390+
E->getLAngleLoc(), E->getRAngleLoc());
73757391
if (!Imp)
73767392
return Imp.takeError();
73777393

73787394
NestedNameSpecifierLoc ToQualifierLoc;
7379-
SourceLocation ToTemplateKeywordLoc, ToExprLoc, ToLAngleLoc, ToRAngleLoc;
7395+
SourceLocation ToTemplateKeywordLoc, ToNameLoc, ToLAngleLoc, ToRAngleLoc;
73807396
DeclarationName ToDeclName;
7381-
std::tie(
7382-
ToQualifierLoc, ToTemplateKeywordLoc, ToDeclName, ToExprLoc,
7383-
ToLAngleLoc, ToRAngleLoc) = *Imp;
7397+
std::tie(ToQualifierLoc, ToTemplateKeywordLoc, ToDeclName, ToNameLoc,
7398+
ToLAngleLoc, ToRAngleLoc) = *Imp;
73847399

7385-
DeclarationNameInfo ToNameInfo(ToDeclName, ToExprLoc);
7400+
DeclarationNameInfo ToNameInfo(ToDeclName, ToNameLoc);
73867401
if (Error Err = ImportDeclarationNameLoc(E->getNameInfo(), ToNameInfo))
73877402
return std::move(Err);
73887403

@@ -7447,7 +7462,7 @@ ASTNodeImporter::VisitUnresolvedLookupExpr(UnresolvedLookupExpr *E) {
74477462
else
74487463
return ToDOrErr.takeError();
74497464

7450-
if (E->hasExplicitTemplateArgs() && E->getTemplateKeywordLoc().isValid()) {
7465+
if (E->hasExplicitTemplateArgs()) {
74517466
TemplateArgumentListInfo ToTAInfo;
74527467
if (Error Err = ImportTemplateArgumentListInfo(
74537468
E->getLAngleLoc(), E->getRAngleLoc(), E->template_arguments(),
@@ -7501,8 +7516,9 @@ ASTNodeImporter::VisitUnresolvedMemberExpr(UnresolvedMemberExpr *E) {
75017516
TemplateArgumentListInfo ToTAInfo;
75027517
TemplateArgumentListInfo *ResInfo = nullptr;
75037518
if (E->hasExplicitTemplateArgs()) {
7504-
if (Error Err =
7505-
ImportTemplateArgumentListInfo(E->template_arguments(), ToTAInfo))
7519+
TemplateArgumentListInfo FromTAInfo;
7520+
E->copyTemplateArgumentsInto(FromTAInfo);
7521+
if (Error Err = ImportTemplateArgumentListInfo(FromTAInfo, ToTAInfo))
75067522
return std::move(Err);
75077523
ResInfo = &ToTAInfo;
75087524
}
@@ -8315,8 +8331,14 @@ ASTImporter::Import(NestedNameSpecifierLoc FromNNS) {
83158331
return std::move(Err);
83168332
TypeSourceInfo *TSI = getToContext().getTrivialTypeSourceInfo(
83178333
QualType(Spec->getAsType(), 0), ToTLoc);
8318-
Builder.Extend(getToContext(), ToLocalBeginLoc, TSI->getTypeLoc(),
8319-
ToLocalEndLoc);
8334+
if (Kind == NestedNameSpecifier::TypeSpecWithTemplate)
8335+
// ToLocalBeginLoc is here the location of the 'template' keyword.
8336+
Builder.Extend(getToContext(), ToLocalBeginLoc, TSI->getTypeLoc(),
8337+
ToLocalEndLoc);
8338+
else
8339+
// No location for 'template' keyword here.
8340+
Builder.Extend(getToContext(), SourceLocation{}, TSI->getTypeLoc(),
8341+
ToLocalEndLoc);
83208342
break;
83218343
}
83228344

clang/test/Import/cxx-anon-namespace/test.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
// CHECK: F.cpp:1:1
99
// The nested anonymous namespace.
1010
// CHECK-NEXT: NamespaceDecl
11-
// CHECK-SAME: <invalid sloc>
11+
// CHECK-SAME: line:21:11
1212
// CHECK: FunctionDecl
1313
// CHECK-SAME: func4
1414
// CHECK-NEXT: CompoundStmt

clang/test/Import/enum/test.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
// RUN: clang-import-test -dump-ast -import %S/Inputs/S.cpp -expression %s | FileCheck %s
22

3+
// CHECK: |-EnumDecl
4+
// CHECK-SAME: Inputs/S.cpp:1:1, line:4:1> line:1:6 E
35
// CHECK: OpaqueWithType 'long'
46

57
void expr() {
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
namespace NS {
2+
void f1();
3+
void f2();
4+
const int A = 3;
5+
}; // namespace NS

clang/test/Import/namespace/test.cpp

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
// RUN: clang-import-test -dump-ast -import %S/Inputs/NS.cpp -expression %s | FileCheck %s
2+
3+
// CHECK: `-NamespaceDecl
4+
// CHECK-SAME: Inputs/NS.cpp:1:1, line:5:1> line:1:11 NS
5+
6+
void expr() {
7+
static_assert(NS::A == 3);
8+
}

clang/test/Import/struct-and-var/test.cpp

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,8 @@
1-
// RUN: clang-import-test --import %S/Inputs/S1.cpp --import %S/Inputs/S2.cpp -expression %s
1+
// RUN: clang-import-test -dump-ast --import %S/Inputs/S1.cpp --import %S/Inputs/S2.cpp -expression %s | FileCheck %s
2+
3+
// CHECK: `-CXXRecordDecl
4+
// CHECK-SAME: Inputs/S2.cpp:1:1, line:3:1> line:1:8 struct F
5+
26
void expr() {
37
struct F f;
48
int x = f.a;

clang/test/Import/template-specialization/test.cpp

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
1-
// RUN: clang-import-test -import %S/Inputs/T.cpp -expression %s
1+
// RUN: clang-import-test -dump-ast -import %S/Inputs/T.cpp -expression %s | FileCheck %s
2+
3+
// CHECK: |-ClassTemplateSpecializationDecl
4+
// CHECK-SAME: <line:4:1, line:8:1> line:4:20 struct A
25

36
void expr() {
47
A<int>::B b1;

0 commit comments

Comments
 (0)