Skip to content

Commit 19e984e

Browse files
committed
Properly print unnamed TagDecl objects in diagnostics
The diagnostics engine is very smart about being passed a NamedDecl to print as part of a diagnostic; it gets the "right" form of the name, quotes it properly, etc. However, the result of using an unnamed tag declaration was to print '' instead of anything useful. This patch causes us to print the same information we'd have gotten if we had printed the type of the declaration rather than the name of it, as that's the most relevant information we can display. Differential Revision: https://reviews.llvm.org/D134813
1 parent ce4d5ae commit 19e984e

32 files changed

+125
-92
lines changed

clang-tools-extra/clangd/unittests/FindTargetTests.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1640,7 +1640,7 @@ TEST_F(FindExplicitReferencesTest, All) {
16401640
int (*$2^fptr)(int $3^a, int) = nullptr;
16411641
}
16421642
)cpp",
1643-
"0: targets = {}\n"
1643+
"0: targets = {(unnamed)}\n"
16441644
"1: targets = {x}, decl\n"
16451645
"2: targets = {fptr}, decl\n"
16461646
"3: targets = {a}, decl\n"},

clang/include/clang/AST/Decl.h

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -291,7 +291,9 @@ class NamedDecl : public Decl {
291291

292292
/// Pretty-print the unqualified name of this declaration. Can be overloaded
293293
/// by derived classes to provide a more user-friendly name when appropriate.
294-
virtual void printName(raw_ostream &os) const;
294+
virtual void printName(raw_ostream &OS, const PrintingPolicy &Policy) const;
295+
/// Calls printName() with the ASTContext printing policy from the decl.
296+
void printName(raw_ostream &OS) const;
295297

296298
/// Get the actual, stored name of the declaration, which may be a special
297299
/// name.
@@ -3654,6 +3656,8 @@ class TagDecl : public TypeDecl,
36543656
return getExtInfo()->TemplParamLists[i];
36553657
}
36563658

3659+
void printName(raw_ostream &OS, const PrintingPolicy &Policy) const override;
3660+
36573661
void setTemplateParameterListsInfo(ASTContext &Context,
36583662
ArrayRef<TemplateParameterList *> TPLists);
36593663

clang/include/clang/AST/DeclCXX.h

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4100,7 +4100,7 @@ class DecompositionDecl final
41004100
return llvm::makeArrayRef(getTrailingObjects<BindingDecl *>(), NumBindings);
41014101
}
41024102

4103-
void printName(raw_ostream &os) const override;
4103+
void printName(raw_ostream &OS, const PrintingPolicy &Policy) const override;
41044104

41054105
static bool classof(const Decl *D) { return classofKind(D->getKind()); }
41064106
static bool classofKind(Kind K) { return K == Decomposition; }
@@ -4213,7 +4213,8 @@ class MSGuidDecl : public ValueDecl,
42134213

42144214
public:
42154215
/// Print this UUID in a human-readable format.
4216-
void printName(llvm::raw_ostream &OS) const override;
4216+
void printName(llvm::raw_ostream &OS,
4217+
const PrintingPolicy &Policy) const override;
42174218

42184219
/// Get the decomposed parts of this declaration.
42194220
Parts getParts() const { return PartVal; }
@@ -4266,7 +4267,8 @@ class UnnamedGlobalConstantDecl : public ValueDecl,
42664267

42674268
public:
42684269
/// Print this in a human-readable format.
4269-
void printName(llvm::raw_ostream &OS) const override;
4270+
void printName(llvm::raw_ostream &OS,
4271+
const PrintingPolicy &Policy) const override;
42704272

42714273
const APValue &getValue() const { return Value; }
42724274

clang/include/clang/AST/DeclTemplate.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3343,7 +3343,8 @@ class TemplateParamObjectDecl : public ValueDecl,
33433343

33443344
public:
33453345
/// Print this template parameter object in a human-readable format.
3346-
void printName(llvm::raw_ostream &OS) const override;
3346+
void printName(llvm::raw_ostream &OS,
3347+
const PrintingPolicy &Policy) const override;
33473348

33483349
/// Print this object as an equivalent expression.
33493350
void printAsExpr(llvm::raw_ostream &OS) const;

clang/lib/AST/ASTDiagnostic.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1893,7 +1893,7 @@ class TemplateDiff {
18931893
TPO->printAsInit(OS, Policy);
18941894
return;
18951895
}
1896-
VD->printName(OS);
1896+
VD->printName(OS, Policy);
18971897
return;
18981898
}
18991899

clang/lib/AST/Decl.cpp

Lines changed: 26 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1602,8 +1602,12 @@ Module *Decl::getOwningModuleForLinkage(bool IgnoreLinkage) const {
16021602
llvm_unreachable("unknown module kind");
16031603
}
16041604

1605-
void NamedDecl::printName(raw_ostream &os) const {
1606-
os << Name;
1605+
void NamedDecl::printName(raw_ostream &OS, const PrintingPolicy&) const {
1606+
OS << Name;
1607+
}
1608+
1609+
void NamedDecl::printName(raw_ostream &OS) const {
1610+
printName(OS, getASTContext().getPrintingPolicy());
16071611
}
16081612

16091613
std::string NamedDecl::getQualifiedNameAsString() const {
@@ -1621,7 +1625,7 @@ void NamedDecl::printQualifiedName(raw_ostream &OS,
16211625
const PrintingPolicy &P) const {
16221626
if (getDeclContext()->isFunctionOrMethod()) {
16231627
// We do not print '(anonymous)' for function parameters without name.
1624-
printName(OS);
1628+
printName(OS, P);
16251629
return;
16261630
}
16271631
printNestedNameSpecifier(OS, P);
@@ -1632,7 +1636,7 @@ void NamedDecl::printQualifiedName(raw_ostream &OS,
16321636
// fall back to "(anonymous)".
16331637
SmallString<64> NameBuffer;
16341638
llvm::raw_svector_ostream NameOS(NameBuffer);
1635-
printName(NameOS);
1639+
printName(NameOS, P);
16361640
if (NameBuffer.empty())
16371641
OS << "(anonymous)";
16381642
else
@@ -1755,7 +1759,7 @@ void NamedDecl::getNameForDiagnostic(raw_ostream &OS,
17551759
if (Qualified)
17561760
printQualifiedName(OS, Policy);
17571761
else
1758-
printName(OS);
1762+
printName(OS, Policy);
17591763
}
17601764

17611765
template<typename T> static bool isRedeclarableImpl(Redeclarable<T> *) {
@@ -4470,6 +4474,23 @@ void TagDecl::setQualifierInfo(NestedNameSpecifierLoc QualifierLoc) {
44704474
}
44714475
}
44724476

4477+
void TagDecl::printName(raw_ostream &OS, const PrintingPolicy &Policy) const {
4478+
DeclarationName Name = getDeclName();
4479+
// If the name is supposed to have an identifier but does not have one, then
4480+
// the tag is anonymous and we should print it differently.
4481+
if (Name.isIdentifier() && !Name.getAsIdentifierInfo()) {
4482+
// If the caller wanted to print a qualified name, they've already printed
4483+
// the scope. And if the caller doesn't want that, the scope information
4484+
// is already printed as part of the type.
4485+
PrintingPolicy Copy(Policy);
4486+
Copy.SuppressScope = true;
4487+
getASTContext().getTagDeclType(this).print(OS, Copy);
4488+
return;
4489+
}
4490+
// Otherwise, do the normal printing.
4491+
Name.print(OS, Policy);
4492+
}
4493+
44734494
void TagDecl::setTemplateParameterListsInfo(
44744495
ASTContext &Context, ArrayRef<TemplateParameterList *> TPLists) {
44754496
assert(!TPLists.empty());

clang/lib/AST/DeclCXX.cpp

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3269,16 +3269,17 @@ DecompositionDecl *DecompositionDecl::CreateDeserialized(ASTContext &C,
32693269
return Result;
32703270
}
32713271

3272-
void DecompositionDecl::printName(llvm::raw_ostream &os) const {
3273-
os << '[';
3272+
void DecompositionDecl::printName(llvm::raw_ostream &OS,
3273+
const PrintingPolicy &Policy) const {
3274+
OS << '[';
32743275
bool Comma = false;
32753276
for (const auto *B : bindings()) {
32763277
if (Comma)
3277-
os << ", ";
3278-
B->printName(os);
3278+
OS << ", ";
3279+
B->printName(OS, Policy);
32793280
Comma = true;
32803281
}
3281-
os << ']';
3282+
OS << ']';
32823283
}
32833284

32843285
void MSPropertyDecl::anchor() {}
@@ -3314,7 +3315,8 @@ MSGuidDecl *MSGuidDecl::CreateDeserialized(ASTContext &C, unsigned ID) {
33143315
return new (C, ID) MSGuidDecl(nullptr, QualType(), Parts());
33153316
}
33163317

3317-
void MSGuidDecl::printName(llvm::raw_ostream &OS) const {
3318+
void MSGuidDecl::printName(llvm::raw_ostream &OS,
3319+
const PrintingPolicy &) const {
33183320
OS << llvm::format("GUID{%08" PRIx32 "-%04" PRIx16 "-%04" PRIx16 "-",
33193321
PartVal.Part1, PartVal.Part2, PartVal.Part3);
33203322
unsigned I = 0;
@@ -3423,7 +3425,8 @@ UnnamedGlobalConstantDecl::CreateDeserialized(ASTContext &C, unsigned ID) {
34233425
UnnamedGlobalConstantDecl(C, nullptr, QualType(), APValue());
34243426
}
34253427

3426-
void UnnamedGlobalConstantDecl::printName(llvm::raw_ostream &OS) const {
3428+
void UnnamedGlobalConstantDecl::printName(llvm::raw_ostream &OS,
3429+
const PrintingPolicy &) const {
34273430
OS << "unnamed-global-constant";
34283431
}
34293432

clang/lib/AST/DeclPrinter.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1711,7 +1711,7 @@ void DeclPrinter::VisitOMPDeclareReductionDecl(OMPDeclareReductionDecl *D) {
17111711
Out << OpName;
17121712
} else {
17131713
assert(D->getDeclName().isIdentifier());
1714-
D->printName(Out);
1714+
D->printName(Out, Policy);
17151715
}
17161716
Out << " : ";
17171717
D->getType().print(Out, Policy);
@@ -1741,7 +1741,7 @@ void DeclPrinter::VisitOMPDeclareReductionDecl(OMPDeclareReductionDecl *D) {
17411741
void DeclPrinter::VisitOMPDeclareMapperDecl(OMPDeclareMapperDecl *D) {
17421742
if (!D->isInvalidDecl()) {
17431743
Out << "#pragma omp declare mapper (";
1744-
D->printName(Out);
1744+
D->printName(Out, Policy);
17451745
Out << " : ";
17461746
D->getType().print(Out, Policy);
17471747
Out << " ";

clang/lib/AST/DeclTemplate.cpp

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1532,9 +1532,10 @@ TemplateParamObjectDecl::CreateDeserialized(ASTContext &C, unsigned ID) {
15321532
return TPOD;
15331533
}
15341534

1535-
void TemplateParamObjectDecl::printName(llvm::raw_ostream &OS) const {
1535+
void TemplateParamObjectDecl::printName(llvm::raw_ostream &OS,
1536+
const PrintingPolicy &Policy) const {
15361537
OS << "<template param ";
1537-
printAsExpr(OS);
1538+
printAsExpr(OS, Policy);
15381539
OS << ">";
15391540
}
15401541

clang/lib/AST/NestedNameSpecifier.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -287,7 +287,7 @@ void NestedNameSpecifier::print(raw_ostream &OS, const PrintingPolicy &Policy,
287287
dyn_cast_or_null<ClassTemplateSpecializationDecl>(getAsRecordDecl());
288288
if (ResolveTemplateArguments && Record) {
289289
// Print the type trait with resolved template parameters.
290-
Record->printName(OS);
290+
Record->printName(OS, Policy);
291291
printTemplateArgumentList(
292292
OS, Record->getTemplateArgs().asArray(), Policy,
293293
Record->getSpecializedTemplate()->getTemplateParameters());

0 commit comments

Comments
 (0)