[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
This commit is contained in:
Balázs Kéri
2019-12-05 16:21:21 +01:00
parent 5c517a6b13
commit a9f10ebffa
7 changed files with 64 additions and 20 deletions

View File

@@ -2228,6 +2228,9 @@ ExpectedDecl ASTNodeImporter::VisitNamespaceDecl(NamespaceDecl *D) {
ExpectedSLoc BeginLocOrErr = import(D->getBeginLoc());
if (!BeginLocOrErr)
return BeginLocOrErr.takeError();
ExpectedSLoc RBraceLocOrErr = import(D->getRBraceLoc());
if (!RBraceLocOrErr)
return RBraceLocOrErr.takeError();
// Create the "to" namespace, if needed.
NamespaceDecl *ToNamespace = MergeWithNamespace;
@@ -2237,6 +2240,7 @@ ExpectedDecl ASTNodeImporter::VisitNamespaceDecl(NamespaceDecl *D) {
*BeginLocOrErr, Loc, Name.getAsIdentifierInfo(),
/*PrevDecl=*/nullptr))
return ToNamespace;
ToNamespace->setRBraceLoc(*RBraceLocOrErr);
ToNamespace->setLexicalDeclContext(LexicalDC);
LexicalDC->addDeclInternal(ToNamespace);
@@ -2545,9 +2549,10 @@ ExpectedDecl ASTNodeImporter::VisitEnumDecl(EnumDecl *D) {
SourceLocation ToBeginLoc;
NestedNameSpecifierLoc ToQualifierLoc;
QualType ToIntegerType;
if (auto Imp = importSeq(
D->getBeginLoc(), D->getQualifierLoc(), D->getIntegerType()))
std::tie(ToBeginLoc, ToQualifierLoc, ToIntegerType) = *Imp;
SourceRange ToBraceRange;
if (auto Imp = importSeq(D->getBeginLoc(), D->getQualifierLoc(),
D->getIntegerType(), D->getBraceRange()))
std::tie(ToBeginLoc, ToQualifierLoc, ToIntegerType, ToBraceRange) = *Imp;
else
return Imp.takeError();
@@ -2561,6 +2566,7 @@ ExpectedDecl ASTNodeImporter::VisitEnumDecl(EnumDecl *D) {
D2->setQualifierInfo(ToQualifierLoc);
D2->setIntegerType(ToIntegerType);
D2->setBraceRange(ToBraceRange);
D2->setAccess(D->getAccess());
D2->setLexicalDeclContext(LexicalDC);
LexicalDC->addDeclInternal(D2);
@@ -2795,6 +2801,10 @@ ExpectedDecl ASTNodeImporter::VisitRecordDecl(RecordDecl *D) {
LexicalDC->addDeclInternal(D2);
}
if (auto BraceRangeOrErr = import(D->getBraceRange()))
D2->setBraceRange(*BraceRangeOrErr);
else
return BraceRangeOrErr.takeError();
if (auto QualifierLocOrErr = import(D->getQualifierLoc()))
D2->setQualifierInfo(*QualifierLocOrErr);
else
@@ -5295,6 +5305,11 @@ ExpectedDecl ASTNodeImporter::VisitClassTemplateSpecializationDecl(
LexicalDC->addDeclInternal(D2);
}
if (auto BraceRangeOrErr = import(D->getBraceRange()))
D2->setBraceRange(*BraceRangeOrErr);
else
return BraceRangeOrErr.takeError();
// Import the qualifier, if any.
if (auto LocOrErr = import(D->getQualifierLoc()))
D2->setQualifierInfo(*LocOrErr);
@@ -6293,7 +6308,8 @@ ExpectedStmt ASTNodeImporter::VisitDeclRefExpr(DeclRefExpr *E) {
TemplateArgumentListInfo *ToResInfo = nullptr;
if (E->hasExplicitTemplateArgs()) {
if (Error Err =
ImportTemplateArgumentListInfo(E->template_arguments(), ToTAInfo))
ImportTemplateArgumentListInfo(E->getLAngleLoc(), E->getRAngleLoc(),
E->template_arguments(), ToTAInfo))
return std::move(Err);
ToResInfo = &ToTAInfo;
}
@@ -7369,20 +7385,19 @@ ExpectedStmt ASTNodeImporter::VisitCXXDependentScopeMemberExpr(
ExpectedStmt
ASTNodeImporter::VisitDependentScopeDeclRefExpr(DependentScopeDeclRefExpr *E) {
auto Imp = importSeq(
E->getQualifierLoc(), E->getTemplateKeywordLoc(), E->getDeclName(),
E->getExprLoc(), E->getLAngleLoc(), E->getRAngleLoc());
auto Imp = importSeq(E->getQualifierLoc(), E->getTemplateKeywordLoc(),
E->getDeclName(), E->getNameInfo().getLoc(),
E->getLAngleLoc(), E->getRAngleLoc());
if (!Imp)
return Imp.takeError();
NestedNameSpecifierLoc ToQualifierLoc;
SourceLocation ToTemplateKeywordLoc, ToExprLoc, ToLAngleLoc, ToRAngleLoc;
SourceLocation ToTemplateKeywordLoc, ToNameLoc, ToLAngleLoc, ToRAngleLoc;
DeclarationName ToDeclName;
std::tie(
ToQualifierLoc, ToTemplateKeywordLoc, ToDeclName, ToExprLoc,
ToLAngleLoc, ToRAngleLoc) = *Imp;
std::tie(ToQualifierLoc, ToTemplateKeywordLoc, ToDeclName, ToNameLoc,
ToLAngleLoc, ToRAngleLoc) = *Imp;
DeclarationNameInfo ToNameInfo(ToDeclName, ToExprLoc);
DeclarationNameInfo ToNameInfo(ToDeclName, ToNameLoc);
if (Error Err = ImportDeclarationNameLoc(E->getNameInfo(), ToNameInfo))
return std::move(Err);
@@ -7447,7 +7462,7 @@ ASTNodeImporter::VisitUnresolvedLookupExpr(UnresolvedLookupExpr *E) {
else
return ToDOrErr.takeError();
if (E->hasExplicitTemplateArgs() && E->getTemplateKeywordLoc().isValid()) {
if (E->hasExplicitTemplateArgs()) {
TemplateArgumentListInfo ToTAInfo;
if (Error Err = ImportTemplateArgumentListInfo(
E->getLAngleLoc(), E->getRAngleLoc(), E->template_arguments(),
@@ -7501,8 +7516,9 @@ ASTNodeImporter::VisitUnresolvedMemberExpr(UnresolvedMemberExpr *E) {
TemplateArgumentListInfo ToTAInfo;
TemplateArgumentListInfo *ResInfo = nullptr;
if (E->hasExplicitTemplateArgs()) {
if (Error Err =
ImportTemplateArgumentListInfo(E->template_arguments(), ToTAInfo))
TemplateArgumentListInfo FromTAInfo;
E->copyTemplateArgumentsInto(FromTAInfo);
if (Error Err = ImportTemplateArgumentListInfo(FromTAInfo, ToTAInfo))
return std::move(Err);
ResInfo = &ToTAInfo;
}
@@ -8315,8 +8331,14 @@ ASTImporter::Import(NestedNameSpecifierLoc FromNNS) {
return std::move(Err);
TypeSourceInfo *TSI = getToContext().getTrivialTypeSourceInfo(
QualType(Spec->getAsType(), 0), ToTLoc);
Builder.Extend(getToContext(), ToLocalBeginLoc, TSI->getTypeLoc(),
ToLocalEndLoc);
if (Kind == NestedNameSpecifier::TypeSpecWithTemplate)
// ToLocalBeginLoc is here the location of the 'template' keyword.
Builder.Extend(getToContext(), ToLocalBeginLoc, TSI->getTypeLoc(),
ToLocalEndLoc);
else
// No location for 'template' keyword here.
Builder.Extend(getToContext(), SourceLocation{}, TSI->getTypeLoc(),
ToLocalEndLoc);
break;
}

View File

@@ -8,7 +8,7 @@
// CHECK: F.cpp:1:1
// The nested anonymous namespace.
// CHECK-NEXT: NamespaceDecl
// CHECK-SAME: <invalid sloc>
// CHECK-SAME: line:21:11
// CHECK: FunctionDecl
// CHECK-SAME: func4
// CHECK-NEXT: CompoundStmt

View File

@@ -1,5 +1,7 @@
// RUN: clang-import-test -dump-ast -import %S/Inputs/S.cpp -expression %s | FileCheck %s
// CHECK: |-EnumDecl
// CHECK-SAME: Inputs/S.cpp:1:1, line:4:1> line:1:6 E
// CHECK: OpaqueWithType 'long'
void expr() {

View File

@@ -0,0 +1,5 @@
namespace NS {
void f1();
void f2();
const int A = 3;
}; // namespace NS

View File

@@ -0,0 +1,8 @@
// RUN: clang-import-test -dump-ast -import %S/Inputs/NS.cpp -expression %s | FileCheck %s
// CHECK: `-NamespaceDecl
// CHECK-SAME: Inputs/NS.cpp:1:1, line:5:1> line:1:11 NS
void expr() {
static_assert(NS::A == 3);
}

View File

@@ -1,4 +1,8 @@
// RUN: clang-import-test --import %S/Inputs/S1.cpp --import %S/Inputs/S2.cpp -expression %s
// RUN: clang-import-test -dump-ast --import %S/Inputs/S1.cpp --import %S/Inputs/S2.cpp -expression %s | FileCheck %s
// CHECK: `-CXXRecordDecl
// CHECK-SAME: Inputs/S2.cpp:1:1, line:3:1> line:1:8 struct F
void expr() {
struct F f;
int x = f.a;

View File

@@ -1,4 +1,7 @@
// RUN: clang-import-test -import %S/Inputs/T.cpp -expression %s
// RUN: clang-import-test -dump-ast -import %S/Inputs/T.cpp -expression %s | FileCheck %s
// CHECK: |-ClassTemplateSpecializationDecl
// CHECK-SAME: <line:4:1, line:8:1> line:4:20 struct A
void expr() {
A<int>::B b1;