[cfe-commits] r81512 - in /cfe/trunk: include/clang/AST/Decl.h include/clang/AST/DeclTemplate.h lib/AST/Decl.cpp lib/AST/DeclTemplate.cpp lib/Sema/Sema.cpp test/CXX/dcl.dcl/dcl.spec/dcl.type/dcl.type.elab/p3.cpp

John McCall rjmccall at apple.com
Thu Sep 10 23:45:03 PDT 2009


Author: rjmccall
Date: Fri Sep 11 01:45:03 2009
New Revision: 81512

URL: http://llvm.org/viewvc/llvm-project?rev=81512&view=rev
Log:
When stringizing a NamedDecl for a diagnostic, treat the template
specialization types differently.


Modified:
    cfe/trunk/include/clang/AST/Decl.h
    cfe/trunk/include/clang/AST/DeclTemplate.h
    cfe/trunk/lib/AST/Decl.cpp
    cfe/trunk/lib/AST/DeclTemplate.cpp
    cfe/trunk/lib/Sema/Sema.cpp
    cfe/trunk/test/CXX/dcl.dcl/dcl.spec/dcl.type/dcl.type.elab/p3.cpp

Modified: cfe/trunk/include/clang/AST/Decl.h
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/include/clang/AST/Decl.h?rev=81512&r1=81511&r2=81512&view=diff

==============================================================================
--- cfe/trunk/include/clang/AST/Decl.h (original)
+++ cfe/trunk/include/clang/AST/Decl.h Fri Sep 11 01:45:03 2009
@@ -123,6 +123,24 @@
   std::string getQualifiedNameAsString() const;
   std::string getQualifiedNameAsString(const PrintingPolicy &Policy) const;
 
+  /// getNameForDiagnostic - Appends a human-readable name for this
+  /// declaration into the given string.
+  ///
+  /// This is the method invoked by Sema when displaying a NamedDecl
+  /// in a diagnostic.  It does not necessarily produce the same
+  /// result as getNameAsString(); for example, class template
+  /// specializations are printed with their template arguments.
+  ///
+  /// TODO: use an API that doesn't require so many temporary strings
+  virtual void getNameForDiagnostic(std::string &S,
+                                    const PrintingPolicy &Policy,
+                                    bool Qualified) const {
+    if (Qualified)
+      S += getQualifiedNameAsString(Policy);
+    else
+      S += getNameAsString();
+  }
+
   /// declarationReplaces - Determine whether this declaration, if
   /// known to be well-formed within its context, will replace the
   /// declaration OldD if introduced into scope. A declaration will
@@ -839,6 +857,10 @@
                               StorageClass S = None, bool isInline = false,
                               bool hasWrittenPrototype = true);
 
+  virtual void getNameForDiagnostic(std::string &S,
+                                    const PrintingPolicy &Policy,
+                                    bool Qualified) const;
+
   virtual SourceRange getSourceRange() const {
     return SourceRange(getLocation(), EndRangeLoc);
   }

Modified: cfe/trunk/include/clang/AST/DeclTemplate.h
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/include/clang/AST/DeclTemplate.h?rev=81512&r1=81511&r2=81512&view=diff

==============================================================================
--- cfe/trunk/include/clang/AST/DeclTemplate.h (original)
+++ cfe/trunk/include/clang/AST/DeclTemplate.h Fri Sep 11 01:45:03 2009
@@ -926,6 +926,10 @@
 
   virtual void Destroy(ASTContext& C);
 
+  virtual void getNameForDiagnostic(std::string &S,
+                                    const PrintingPolicy &Policy,
+                                    bool Qualified) const;
+
   /// \brief Retrieve the template that this specialization specializes.
   ClassTemplateDecl *getSpecializedTemplate() const;
 

Modified: cfe/trunk/lib/AST/Decl.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/AST/Decl.cpp?rev=81512&r1=81511&r2=81512&view=diff

==============================================================================
--- cfe/trunk/lib/AST/Decl.cpp (original)
+++ cfe/trunk/lib/AST/Decl.cpp Fri Sep 11 01:45:03 2009
@@ -404,6 +404,18 @@
   Decl::Destroy(C);
 }
 
+void FunctionDecl::getNameForDiagnostic(std::string &S,
+                                        const PrintingPolicy &Policy,
+                                        bool Qualified) const {
+  NamedDecl::getNameForDiagnostic(S, Policy, Qualified);
+  const TemplateArgumentList *TemplateArgs = getTemplateSpecializationArgs();
+  if (TemplateArgs)
+    S += TemplateSpecializationType::PrintTemplateArgumentList(
+                                         TemplateArgs->getFlatArgumentList(),
+                                         TemplateArgs->flat_size(),
+                                                               Policy);
+    
+}
 
 Stmt *FunctionDecl::getBody(const FunctionDecl *&Definition) const {
   for (redecl_iterator I = redecls_begin(), E = redecls_end(); I != E; ++I) {

Modified: cfe/trunk/lib/AST/DeclTemplate.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/AST/DeclTemplate.cpp?rev=81512&r1=81511&r2=81512&view=diff

==============================================================================
--- cfe/trunk/lib/AST/DeclTemplate.cpp (original)
+++ cfe/trunk/lib/AST/DeclTemplate.cpp Fri Sep 11 01:45:03 2009
@@ -422,6 +422,19 @@
   CXXRecordDecl::Destroy(C);
 }
 
+void
+ClassTemplateSpecializationDecl::getNameForDiagnostic(std::string &S,
+                                                  const PrintingPolicy &Policy,
+                                                      bool Qualified) const {
+  NamedDecl::getNameForDiagnostic(S, Policy, Qualified);
+
+  const TemplateArgumentList &TemplateArgs = getTemplateArgs();
+  S += TemplateSpecializationType::PrintTemplateArgumentList(
+                                       TemplateArgs.getFlatArgumentList(),
+                                       TemplateArgs.flat_size(),
+                                                             Policy);
+}
+
 ClassTemplateDecl *
 ClassTemplateSpecializationDecl::getSpecializedTemplate() const {
   if (SpecializedPartialSpecialization *PartialSpec

Modified: cfe/trunk/lib/Sema/Sema.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Sema/Sema.cpp?rev=81512&r1=81511&r2=81512&view=diff

==============================================================================
--- cfe/trunk/lib/Sema/Sema.cpp (original)
+++ cfe/trunk/lib/Sema/Sema.cpp Fri Sep 11 01:45:03 2009
@@ -85,13 +85,17 @@
       assert(ModLen == 0 && ArgLen == 0 &&
              "Invalid modifier for DeclarationName argument");
   } else if (Kind == Diagnostic::ak_nameddecl) {
+    bool Qualified;
     if (ModLen == 1 && Modifier[0] == 'q' && ArgLen == 0)
-      S = reinterpret_cast<NamedDecl*>(Val)->getQualifiedNameAsString();
+      Qualified = true;
     else {
       assert(ModLen == 0 && ArgLen == 0 &&
            "Invalid modifier for NamedDecl* argument");
-      S = reinterpret_cast<NamedDecl*>(Val)->getNameAsString();
+      Qualified = false;
     }
+    reinterpret_cast<NamedDecl*>(Val)->getNameForDiagnostic(S,
+                                                         Context.PrintingPolicy,
+                                                            Qualified);
   } else {
     llvm::raw_string_ostream OS(S);
     assert(Kind == Diagnostic::ak_nestednamespec);

Modified: cfe/trunk/test/CXX/dcl.dcl/dcl.spec/dcl.type/dcl.type.elab/p3.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/test/CXX/dcl.dcl/dcl.spec/dcl.type/dcl.type.elab/p3.cpp?rev=81512&r1=81511&r2=81512&view=diff

==============================================================================
--- cfe/trunk/test/CXX/dcl.dcl/dcl.spec/dcl.type/dcl.type.elab/p3.cpp (original)
+++ cfe/trunk/test/CXX/dcl.dcl/dcl.spec/dcl.type/dcl.type.elab/p3.cpp Fri Sep 11 01:45:03 2009
@@ -53,7 +53,7 @@
 void e4(enum B<A>::Member); // expected-error {{use of 'Member' with tag type that does not match previous declaration}}
 
 template <class T> struct C {
-  void foo(class B<T>::Member); // expected-error{{no type named 'Member' in 'B'}}
+  void foo(class B<T>::Member); // expected-error{{no type named 'Member' in 'B<int>'}}
 };
 
 C<float> f1;





More information about the cfe-commits mailing list