From: Douglas Gregor Date: Mon, 5 Apr 2010 21:25:31 +0000 (+0000) Subject: Extend the type printing policy to allow one to turn off the printing X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=84139d6ef8967cfdb70d37378a7a65cc4827d44d;p=clang Extend the type printing policy to allow one to turn off the printing of file locations for anonymous tag types (e.g., "enum "), which can get rather long. git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@100470 91177308-0d34-0410-b5e6-96231b3b80d8 --- diff --git a/include/clang/AST/PrettyPrinter.h b/include/clang/AST/PrettyPrinter.h index 587b5c2b40..70d65d35fe 100644 --- a/include/clang/AST/PrettyPrinter.h +++ b/include/clang/AST/PrettyPrinter.h @@ -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 "). Otherwise, just + /// prints "" for the name. + bool AnonymousTagLocations : 1; }; } // end namespace clang diff --git a/lib/AST/TypePrinter.cpp b/lib/AST/TypePrinter.cpp index 4cf0922ee3..340e373af1 100644 --- a/lib/AST/TypePrinter.cpp +++ b/lib/AST/TypePrinter.cpp @@ -442,18 +442,21 @@ void TypePrinter::PrintTag(TagDecl *D, std::string &InnerString) { llvm::raw_string_ostream OS(Buffer); OS << "getKindName(); - - PresumedLoc PLoc = D->getASTContext().getSourceManager().getPresumedLoc( - D->getLocation()); - OS << " at " << PLoc.getFilename() - << ':' << PLoc.getLine() - << ':' << PLoc.getColumn() - << '>'; + 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(); + } + + OS << '>'; OS.flush(); } diff --git a/lib/Sema/SemaCodeComplete.cpp b/lib/Sema/SemaCodeComplete.cpp index 317eef8d60..df14aa7fc5 100644 --- a/lib/Sema/SemaCodeComplete.cpp +++ b/lib/Sema/SemaCodeComplete.cpp @@ -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); } diff --git a/test/Index/complete-enums.c b/test/Index/complete-enums.c new file mode 100644 index 0000000000..5e712a1122 --- /dev/null +++ b/test/Index/complete-enums.c @@ -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 }{TypedText Red}