[llvm] r264360 - Try to fix ODR violation of ErrorInfo::ID

Reid Kleckner via llvm-commits llvm-commits at lists.llvm.org
Thu Mar 24 16:49:35 PDT 2016


Author: rnk
Date: Thu Mar 24 18:49:34 2016
New Revision: 264360

URL: http://llvm.org/viewvc/llvm-project?rev=264360&view=rev
Log:
Try to fix ODR violation of ErrorInfo::ID

This implements my suggestion to Lang.

Modified:
    llvm/trunk/docs/ProgrammersManual.rst
    llvm/trunk/include/llvm/Support/Error.h
    llvm/trunk/lib/Support/Error.cpp
    llvm/trunk/unittests/Support/ErrorTest.cpp

Modified: llvm/trunk/docs/ProgrammersManual.rst
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/docs/ProgrammersManual.rst?rev=264360&r1=264359&r2=264360&view=diff
==============================================================================
--- llvm/trunk/docs/ProgrammersManual.rst (original)
+++ llvm/trunk/docs/ProgrammersManual.rst Thu Mar 24 18:49:34 2016
@@ -342,10 +342,13 @@ that inherits from the ErrorInfo utility
   public:
     MyError(std::string Msg) : Msg(Msg) {}
     void log(OStream &OS) const override { OS << "MyError - " << Msg; }
+    static char ID;
   private:
     std::string Msg;
   };
 
+  char MyError::ID = 0; // In MyError.cpp
+
   Error bar() {
     if (checkErrorCondition)
       return make_error<MyError>("Error condition detected");

Modified: llvm/trunk/include/llvm/Support/Error.h
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/include/llvm/Support/Error.h?rev=264360&r1=264359&r2=264360&view=diff
==============================================================================
--- llvm/trunk/include/llvm/Support/Error.h (original)
+++ llvm/trunk/include/llvm/Support/Error.h Thu Mar 24 18:49:34 2016
@@ -285,15 +285,9 @@ public:
     return ClassID == classID() || ParentErrT::isA(ClassID);
   }
 
-  static const void *classID() { return &ID; }
-
-private:
-  static char ID;
+  static const void *classID() { return &ThisErrT::ID; }
 };
 
-template <typename ThisErrT, typename ParentErrT>
-char ErrorInfo<ThisErrT, ParentErrT>::ID = 0;
-
 /// Special ErrorInfo subclass representing a list of ErrorInfos.
 /// Instances of this class are constructed by joinError.
 class ErrorList final : public ErrorInfo<ErrorList> {
@@ -317,6 +311,9 @@ public:
 
   std::error_code convertToErrorCode() const override;
 
+  // Used by ErrorInfo::classID.
+  static char ID;
+
 private:
   ErrorList(std::unique_ptr<ErrorInfoBase> Payload1,
             std::unique_ptr<ErrorInfoBase> Payload2) {
@@ -729,6 +726,9 @@ public:
   std::error_code convertToErrorCode() const override { return EC; }
   void log(raw_ostream &OS) const override { OS << EC.message(); }
 
+  // Used by ErrorInfo::classID.
+  static char ID;
+
 protected:
   std::error_code EC;
 };

Modified: llvm/trunk/lib/Support/Error.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/Support/Error.cpp?rev=264360&r1=264359&r2=264360&view=diff
==============================================================================
--- llvm/trunk/lib/Support/Error.cpp (original)
+++ llvm/trunk/lib/Support/Error.cpp Thu Mar 24 18:49:34 2016
@@ -36,9 +36,8 @@ namespace {
 
 void ErrorInfoBase::anchor() {}
 char ErrorInfoBase::ID = 0;
-
-template <> char ErrorInfo<ErrorList>::ID = 0;
-template <> char ErrorInfo<ECError>::ID = 0;
+char ErrorList::ID = 0;
+char ECError::ID = 0;
 
 static ManagedStatic<ErrorErrorCategory> ErrorErrorCat;
 

Modified: llvm/trunk/unittests/Support/ErrorTest.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/unittests/Support/ErrorTest.cpp?rev=264360&r1=264359&r2=264360&view=diff
==============================================================================
--- llvm/trunk/unittests/Support/ErrorTest.cpp (original)
+++ llvm/trunk/unittests/Support/ErrorTest.cpp Thu Mar 24 18:49:34 2016
@@ -35,6 +35,9 @@ public:
     llvm_unreachable("CustomError doesn't support ECError conversion");
   }
 
+  // Used by ErrorInfo::classID.
+  static char ID;
+
 protected:
   // This error is subclassed below, but we can't use inheriting constructors
   // yet, so we can't propagate the constructors through ErrorInfo. Instead
@@ -45,6 +48,8 @@ protected:
   int Info;
 };
 
+char CustomError::ID = 0;
+
 // Custom error class with a custom base class and some additional random
 // 'info'.
 class CustomSubError : public ErrorInfo<CustomSubError, CustomError> {
@@ -66,10 +71,15 @@ public:
     llvm_unreachable("CustomSubError doesn't support ECError conversion");
   }
 
+  // Used by ErrorInfo::classID.
+  static char ID;
+
 protected:
   int ExtraInfo;
 };
 
+char CustomSubError::ID = 0;
+
 static Error handleCustomError(const CustomError &CE) { return Error(); }
 
 static void handleCustomErrorVoid(const CustomError &CE) {}
@@ -453,6 +463,3 @@ TEST(Error, ErrorCodeConversions) {
 }
 
 } // end anon namespace
-
-template <> char ErrorInfo<CustomError>::ID = 0;
-template <> char ErrorInfo<CustomSubError, CustomError>::ID = 0;




More information about the llvm-commits mailing list