[cfe-commits] r84602 - in /cfe/trunk: include/clang/Basic/Diagnostic.h lib/Basic/Diagnostic.cpp lib/Sema/Sema.cpp

Chris Lattner sabre at nondot.org
Mon Oct 19 22:25:23 PDT 2009


Author: lattner
Date: Tue Oct 20 00:25:22 2009
New Revision: 84602

URL: http://llvm.org/viewvc/llvm-project?rev=84602&view=rev
Log:
teach FormatDiagnostic to aggregate previously formatted arguments and
pass them down into the ArgToStringFn implementation.  This allows 
redundancy across operands to a diagnostic to be eliminated.

This isn't used yet, so no functionality change.

Modified:
    cfe/trunk/include/clang/Basic/Diagnostic.h
    cfe/trunk/lib/Basic/Diagnostic.cpp
    cfe/trunk/lib/Sema/Sema.cpp

Modified: cfe/trunk/include/clang/Basic/Diagnostic.h
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/include/clang/Basic/Diagnostic.h?rev=84602&r1=84601&r2=84602&view=diff

==============================================================================
--- cfe/trunk/include/clang/Basic/Diagnostic.h (original)
+++ cfe/trunk/include/clang/Basic/Diagnostic.h Tue Oct 20 00:25:22 2009
@@ -164,6 +164,10 @@
     ak_nestednamespec,  // NestedNameSpecifier *
     ak_declcontext      // DeclContext *
   };
+  
+  /// ArgumentValue - This typedef represents on argument value, which is a
+  /// union discriminated by ArgumentKind, with a value.
+  typedef std::pair<ArgumentKind, intptr_t> ArgumentValue;
 
 private:
   unsigned char AllExtensionsSilenced; // Used by __extension__
@@ -203,10 +207,17 @@
   /// ArgToStringFn - A function pointer that converts an opaque diagnostic
   /// argument to a strings.  This takes the modifiers and argument that was
   /// present in the diagnostic.
+  ///
+  /// The PrevArgs array (whose length is NumPrevArgs) indicates the previous
+  /// arguments formatted for this diagnostic.  Implementations of this function
+  /// can use this information to avoid redundancy across arguments.
+  ///
   /// This is a hack to avoid a layering violation between libbasic and libsema.
   typedef void (*ArgToStringFnTy)(ArgumentKind Kind, intptr_t Val,
                                   const char *Modifier, unsigned ModifierLen,
                                   const char *Argument, unsigned ArgumentLen,
+                                  const ArgumentValue *PrevArgs,
+                                  unsigned NumPrevArgs,
                                   llvm::SmallVectorImpl<char> &Output,
                                   void *Cookie);
   void *ArgToStringCookie;
@@ -311,9 +322,10 @@
   void ConvertArgToString(ArgumentKind Kind, intptr_t Val,
                           const char *Modifier, unsigned ModLen,
                           const char *Argument, unsigned ArgLen,
+                          const ArgumentValue *PrevArgs, unsigned NumPrevArgs,
                           llvm::SmallVectorImpl<char> &Output) const {
-    ArgToStringFn(Kind, Val, Modifier, ModLen, Argument, ArgLen, Output,
-                  ArgToStringCookie);
+    ArgToStringFn(Kind, Val, Modifier, ModLen, Argument, ArgLen,
+                  PrevArgs, NumPrevArgs, Output, ArgToStringCookie);
   }
 
   void SetArgToStringFn(ArgToStringFnTy Fn, void *Cookie) {

Modified: cfe/trunk/lib/Basic/Diagnostic.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Basic/Diagnostic.cpp?rev=84602&r1=84601&r2=84602&view=diff

==============================================================================
--- cfe/trunk/lib/Basic/Diagnostic.cpp (original)
+++ cfe/trunk/lib/Basic/Diagnostic.cpp Tue Oct 20 00:25:22 2009
@@ -190,6 +190,8 @@
 static void DummyArgToStringFn(Diagnostic::ArgumentKind AK, intptr_t QT,
                                const char *Modifier, unsigned ML,
                                const char *Argument, unsigned ArgLen,
+                               const Diagnostic::ArgumentValue *PrevArgs,
+                               unsigned NumPrevArgs,
                                llvm::SmallVectorImpl<char> &Output,
                                void *Cookie) {
   const char *Str = "<can't format argument>";
@@ -685,6 +687,12 @@
   const char *DiagStr = getDiags()->getDescription(getID());
   const char *DiagEnd = DiagStr+strlen(DiagStr);
 
+  /// FormattedArgs - Keep track of all of the arguments formatted by
+  /// ConvertArgToString and pass them into subsequent calls to
+  /// ConvertArgToString, allowing the implementation to avoid redundancies in
+  /// obvious cases.
+  llvm::SmallVector<Diagnostic::ArgumentValue, 8> FormattedArgs;
+  
   while (DiagStr != DiagEnd) {
     if (DiagStr[0] != '%') {
       // Append non-%0 substrings to Str if we have one.
@@ -732,7 +740,9 @@
     assert(isdigit(*DiagStr) && "Invalid format for argument in diagnostic");
     unsigned ArgNo = *DiagStr++ - '0';
 
-    switch (getArgKind(ArgNo)) {
+    Diagnostic::ArgumentKind Kind = getArgKind(ArgNo);
+    
+    switch (Kind) {
     // ---- STRINGS ----
     case Diagnostic::ak_std_string: {
       const std::string &S = getArgStdStr(ArgNo);
@@ -802,11 +812,23 @@
     case Diagnostic::ak_nameddecl:
     case Diagnostic::ak_nestednamespec:
     case Diagnostic::ak_declcontext:
-      getDiags()->ConvertArgToString(getArgKind(ArgNo), getRawArg(ArgNo),
+      getDiags()->ConvertArgToString(Kind, getRawArg(ArgNo),
                                      Modifier, ModifierLen,
-                                     Argument, ArgumentLen, OutStr);
+                                     Argument, ArgumentLen,
+                                     FormattedArgs.data(), FormattedArgs.size(),
+                                     OutStr);
       break;
     }
+    
+    // Remember this argument info for subsequent formatting operations.  Turn
+    // std::strings into a null terminated string to make it be the same case as
+    // all the other ones.
+    if (Kind != Diagnostic::ak_std_string)
+      FormattedArgs.push_back(std::make_pair(Kind, getRawArg(ArgNo)));
+    else
+      FormattedArgs.push_back(std::make_pair(Diagnostic::ak_c_string,
+                                        (intptr_t)getArgStdStr(ArgNo).c_str()));
+    
   }
 }
 

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

==============================================================================
--- cfe/trunk/lib/Sema/Sema.cpp (original)
+++ cfe/trunk/lib/Sema/Sema.cpp Tue Oct 20 00:25:22 2009
@@ -148,6 +148,8 @@
 static void ConvertArgToStringFn(Diagnostic::ArgumentKind Kind, intptr_t Val,
                                  const char *Modifier, unsigned ModLen,
                                  const char *Argument, unsigned ArgLen,
+                                 const Diagnostic::ArgumentValue *PrevArgs,
+                                 unsigned NumPrevArgs,
                                  llvm::SmallVectorImpl<char> &Output,
                                  void *Cookie) {
   ASTContext &Context = *static_cast<ASTContext*>(Cookie);





More information about the cfe-commits mailing list