[cfe-commits] r44498 - in /cfe/trunk: Basic/Diagnostic.cpp Driver/RewriteTest.cpp include/clang/Basic/Diagnostic.h

Chris Lattner sabre at nondot.org
Sat Dec 1 17:09:57 PST 2007


Author: lattner
Date: Sat Dec  1 19:09:57 2007
New Revision: 44498

URL: http://llvm.org/viewvc/llvm-project?rev=44498&view=rev
Log:
add support for custom client-specific diagnostics.  As a testcase, make the 
rewriter emit this error if it fails to rewrite an @encode:

t.m:17:9: error: rewriter could not replace sub-expression due to macros
    c = ENC(char *)[2] + 4;
        ^~~~~~~~~~~

.. where ENC is: #define ENC @encode


Modified:
    cfe/trunk/Basic/Diagnostic.cpp
    cfe/trunk/Driver/RewriteTest.cpp
    cfe/trunk/include/clang/Basic/Diagnostic.h

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

==============================================================================
--- cfe/trunk/Basic/Diagnostic.cpp (original)
+++ cfe/trunk/Basic/Diagnostic.cpp Sat Dec  1 19:09:57 2007
@@ -14,8 +14,14 @@
 #include "clang/Basic/Diagnostic.h"
 #include "clang/Basic/SourceLocation.h"
 #include <cassert>
+#include <vector>
+#include <map>
 using namespace clang;
 
+//===----------------------------------------------------------------------===//
+// Builtin Diagnostic information
+//===----------------------------------------------------------------------===//
+
 /// Flag values for diagnostics.
 enum {
   // Diagnostic classes.
@@ -50,6 +56,56 @@
   0
 };
 
+//===----------------------------------------------------------------------===//
+// Custom Diagnostic information
+//===----------------------------------------------------------------------===//
+
+namespace clang {
+  namespace diag {
+    class CustomDiagInfo {
+      typedef std::pair<Diagnostic::Level, std::string> DiagDesc;
+      std::vector<DiagDesc> DiagInfo;
+      std::map<DiagDesc, unsigned> DiagIDs;
+    public:
+      
+      /// getDescription - Return the description of the specified custom
+      /// diagnostic.
+      const char *getDescription(unsigned DiagID) const {
+        assert(this && DiagID-diag::NUM_BUILTIN_DIAGNOSTICS < DiagInfo.size() &&
+               "Invalid diagnosic ID");
+        return DiagInfo[DiagID-diag::NUM_BUILTIN_DIAGNOSTICS].second.c_str();
+      }
+      
+      /// getLevel - Return the level of the specified custom diagnostic.
+      Diagnostic::Level getLevel(unsigned DiagID) const {
+        assert(this && DiagID-diag::NUM_BUILTIN_DIAGNOSTICS < DiagInfo.size() &&
+               "Invalid diagnosic ID");
+        return DiagInfo[DiagID-diag::NUM_BUILTIN_DIAGNOSTICS].first;
+      }
+      
+      unsigned getOrCreateDiagID(Diagnostic::Level L, const char *Message) {
+        DiagDesc D(L, Message);
+        // Check to see if it already exists.
+        std::map<DiagDesc, unsigned>::iterator I = DiagIDs.lower_bound(D);
+        if (I != DiagIDs.end() && I->first == D)
+          return I->second;
+        
+        // If not, assign a new ID.
+        unsigned ID = DiagInfo.size()+diag::NUM_BUILTIN_DIAGNOSTICS;
+        DiagIDs.insert(std::make_pair(D, ID));
+        DiagInfo.push_back(D);
+        return ID;
+      }
+    };
+    
+  } // end diag namespace 
+} // end clang namespace 
+
+
+//===----------------------------------------------------------------------===//
+// Common Diagnostic implementation
+//===----------------------------------------------------------------------===//
+
 Diagnostic::Diagnostic(DiagnosticClient &client) : Client(client) {
   WarningsAsErrors = false;
   WarnOnExtensions = false;
@@ -60,8 +116,23 @@
   ErrorOccurred = false;
   NumDiagnostics = 0;
   NumErrors = 0;
+  CustomDiagInfo = 0;
+}
+
+Diagnostic::~Diagnostic() {
+  delete CustomDiagInfo;
 }
 
+/// getCustomDiagID - Return an ID for a diagnostic with the specified message
+/// and level.  If this is the first request for this diagnosic, it is
+/// registered and created, otherwise the existing ID is returned.
+unsigned Diagnostic::getCustomDiagID(Level L, const char *Message) {
+  if (CustomDiagInfo == 0) 
+    CustomDiagInfo = new diag::CustomDiagInfo();
+  return CustomDiagInfo->getOrCreateDiagID(L, Message);
+}
+
+
 /// isBuiltinNoteWarningOrExtension - Return true if the unmapped diagnostic
 /// level of the specified diagnostic ID is a Note, Warning, or Extension.
 /// Note that this only works on builtin diagnostics, not custom ones.
@@ -77,17 +148,16 @@
   if (DiagID < diag::NUM_BUILTIN_DIAGNOSTICS)
     return DiagnosticText[DiagID];
   else 
-    assert(0 && "FIXME: IMPLEMENT");
+    return CustomDiagInfo->getDescription(DiagID);
 }
 
 /// getDiagnosticLevel - Based on the way the client configured the Diagnostic
 /// object, classify the specified diagnostic ID into a Level, consumable by
 /// the DiagnosticClient.
 Diagnostic::Level Diagnostic::getDiagnosticLevel(unsigned DiagID) const {
-  if (DiagID >= diag::NUM_BUILTIN_DIAGNOSTICS) {
-    // FIXME: HAndle custom here.
-    assert(0 && "unimp");
-  }
+  // Handle custom diagnostics, which cannot be mapped.
+  if (DiagID >= diag::NUM_BUILTIN_DIAGNOSTICS)
+    return CustomDiagInfo->getLevel(DiagID);
   
   unsigned DiagClass = getBuiltinDiagClass(DiagID);
   

Modified: cfe/trunk/Driver/RewriteTest.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/Driver/RewriteTest.cpp?rev=44498&r1=44497&r2=44498&view=diff

==============================================================================
--- cfe/trunk/Driver/RewriteTest.cpp (original)
+++ cfe/trunk/Driver/RewriteTest.cpp Sat Dec  1 19:09:57 2007
@@ -867,6 +867,11 @@
                                         SourceLocation(), SourceLocation());
   if (Rewrite.ReplaceStmt(Exp, Replacement)) {
     // replacement failed.
+    unsigned DiagID = Diags.getCustomDiagID(Diagnostic::Error, 
+                     "rewriter could not replace sub-expression due to macros");
+    SourceRange Range = Exp->getSourceRange();
+    Diags.Report(Exp->getAtLoc(), DiagID, 0, 0, &Range, 1);
+    delete Replacement;
     return Exp;
   }
   

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

==============================================================================
--- cfe/trunk/include/clang/Basic/Diagnostic.h (original)
+++ cfe/trunk/include/clang/Basic/Diagnostic.h Sat Dec  1 19:09:57 2007
@@ -24,6 +24,8 @@
   
   // Import the diagnostic enums themselves.
   namespace diag {
+    class CustomDiagInfo;
+    
     /// diag::kind - All of the diagnostics that can be emitted by the frontend.
     enum kind {
 #define DIAG(ENUM,FLAGS,DESC) ENUM,
@@ -47,6 +49,13 @@
 /// "report warnings as errors" and passes them off to the DiagnosticClient for
 /// reporting to the user.
 class Diagnostic {
+public:
+  /// Level - The level of the diagnostic, after it has been through mapping.
+  enum Level {
+    Ignored, Note, Warning, Error, Fatal
+  };
+  
+private:  
   bool WarningsAsErrors;      // Treat warnings like errors: 
   bool WarnOnExtensions;      // Enables warnings for gcc extensions: -pedantic.
   bool ErrorOnExtensions;     // Error on extensions: -pedantic-errors.
@@ -62,8 +71,12 @@
 
   unsigned NumDiagnostics;    // Number of diagnostics reported
   unsigned NumErrors;         // Number of diagnostics that are errors
+
+  /// CustomDiagInfo - Information for uniquing and looking up custom diags.
+  diag::CustomDiagInfo *CustomDiagInfo;
 public:
   explicit Diagnostic(DiagnosticClient &client);
+  ~Diagnostic();
   
   //===--------------------------------------------------------------------===//
   //  Diagnostic characterization methods, used by a client to customize how
@@ -108,6 +121,11 @@
   unsigned getNumErrors() const { return NumErrors; }
   unsigned getNumDiagnostics() const { return NumDiagnostics; }
   
+  /// getCustomDiagID - Return an ID for a diagnostic with the specified message
+  /// and level.  If this is the first request for this diagnosic, it is
+  /// registered and created, otherwise the existing ID is returned.
+  unsigned getCustomDiagID(Level L, const char *Message);
+  
   //===--------------------------------------------------------------------===//
   // Diagnostic classification and reporting interfaces.
   //
@@ -116,11 +134,6 @@
   /// issue.
   const char *getDescription(unsigned DiagID);
   
-  /// Level - The level of the diagnostic, after it has been through mapping.
-  enum Level {
-    Ignored, Note, Warning, Error, Fatal
-  };
-  
   /// isBuiltinNoteWarningOrExtension - Return true if the unmapped diagnostic
   /// level of the specified diagnostic ID is a Note, Warning, or Extension.
   /// Note that this only works on builtin diagnostics, not custom ones.





More information about the cfe-commits mailing list