Extend the type printing policy to allow one to turn off the printing

of file locations for anonymous tag types (e.g., "enum <anonymous at
t.h:15:6>"), which can get rather long.

llvm-svn: 100470
This commit is contained in:
Douglas Gregor 2010-04-05 21:25:31 +00:00
parent 6e085c1efe
commit cf04b02d1d
4 changed files with 40 additions and 14 deletions

View File

@ -37,7 +37,8 @@ struct PrintingPolicy {
PrintingPolicy(const LangOptions &LO)
: Indentation(2), LangOpts(LO), SuppressSpecifiers(false),
SuppressTag(false), SuppressScope(false),
Dump(false), ConstantArraySizeAsWritten(false) { }
Dump(false), ConstantArraySizeAsWritten(false),
AnonymousTagLocations(true) { }
/// \brief The number of spaces to use to indent each line.
unsigned Indentation : 8;
@ -97,7 +98,11 @@ struct PrintingPolicy {
/// char a[9] = "A string";
/// \endcode
bool ConstantArraySizeAsWritten : 1;
/// \brief When printing an anonymous tag name, also print the location of
/// that entity (e.g., "enum <anonymous at t.h:10:5>"). Otherwise, just
/// prints "<anonymous>" for the name.
bool AnonymousTagLocations : 1;
};
} // end namespace clang

View File

@ -442,18 +442,21 @@ void TypePrinter::PrintTag(TagDecl *D, std::string &InnerString) {
llvm::raw_string_ostream OS(Buffer);
OS << "<anonymous";
// Suppress the redundant tag keyword if we just printed one.
// We don't have to worry about ElaboratedTypes here because you can't
// refer to an anonymous type with one.
if (!HasKindDecoration)
OS << " " << D->getKindName();
if (Policy.AnonymousTagLocations) {
// Suppress the redundant tag keyword if we just printed one.
// We don't have to worry about ElaboratedTypes here because you can't
// refer to an anonymous type with one.
if (!HasKindDecoration)
OS << " " << D->getKindName();
PresumedLoc PLoc = D->getASTContext().getSourceManager().getPresumedLoc(
D->getLocation());
OS << " at " << PLoc.getFilename()
<< ':' << PLoc.getLine()
<< ':' << PLoc.getColumn()
<< '>';
PresumedLoc PLoc = D->getASTContext().getSourceManager().getPresumedLoc(
D->getLocation());
OS << " at " << PLoc.getFilename()
<< ':' << PLoc.getLine()
<< ':' << PLoc.getColumn();
}
OS << '>';
OS.flush();
}

View File

@ -1338,8 +1338,11 @@ static void AddResultTypeChunk(ASTContext &Context,
if (T.isNull() || Context.hasSameType(T, Context.DependentTy))
return;
PrintingPolicy Policy(Context.PrintingPolicy);
Policy.AnonymousTagLocations = false;
std::string TypeStr;
T.getAsStringInternal(TypeStr, Context.PrintingPolicy);
T.getAsStringInternal(TypeStr, Policy);
Result->AddResultTypeChunk(TypeStr);
}

View File

@ -0,0 +1,15 @@
// Note: the run lines follow their respective tests, since line/column
// matter in this test.
enum {
Red = 17,
Green,
Blue
};
void f() {
}
// RUN: c-index-test -code-completion-at=%s:11:1 %s | FileCheck -check-prefix=CHECK-CC1 %s
// CHECK-CC1: EnumConstantDecl:{ResultType enum <anonymous>}{TypedText Red}