[cfe-commits] r116153 - in /cfe/trunk: include/clang/Sema/DelayedDiagnostic.h include/clang/Sema/Sema.h lib/Sema/SemaDeclAttr.cpp lib/Sema/SemaExpr.cpp

Benjamin Kramer benny.kra at googlemail.com
Sat Oct 9 08:49:00 PDT 2010


Author: d0k
Date: Sat Oct  9 10:49:00 2010
New Revision: 116153

URL: http://llvm.org/viewvc/llvm-project?rev=116153&view=rev
Log:
Don't rely on a StringRef being null-terminated (it's not) for deprecation messages.
Store pointer and length of the message in DelayedDiagnostic and hide the gory union details.

Modified:
    cfe/trunk/include/clang/Sema/DelayedDiagnostic.h
    cfe/trunk/include/clang/Sema/Sema.h
    cfe/trunk/lib/Sema/SemaDeclAttr.cpp
    cfe/trunk/lib/Sema/SemaExpr.cpp

Modified: cfe/trunk/include/clang/Sema/DelayedDiagnostic.h
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/include/clang/Sema/DelayedDiagnostic.h?rev=116153&r1=116152&r2=116153&view=diff
==============================================================================
--- cfe/trunk/include/clang/Sema/DelayedDiagnostic.h (original)
+++ cfe/trunk/include/clang/Sema/DelayedDiagnostic.h Sat Oct  9 10:49:00 2010
@@ -119,14 +119,6 @@
 
   SourceLocation Loc;
 
-  union {
-    /// Deprecation.
-    struct { NamedDecl *Decl; const char* Message; } DeprecationData;
-
-    /// Access control.
-    char AccessData[sizeof(AccessedEntity)];
-  };
-
   void destroy() {
     switch (Kind) {
     case Access: getAccessData().~AccessedEntity(); break;
@@ -135,14 +127,15 @@
   }
 
   static DelayedDiagnostic makeDeprecation(SourceLocation Loc,
-                                           NamedDecl *D,
-                                           const char *Msg) {
+                                           const NamedDecl *D,
+                                           llvm::StringRef Msg) {
     DelayedDiagnostic DD;
     DD.Kind = Deprecation;
     DD.Triggered = false;
     DD.Loc = Loc;
     DD.DeprecationData.Decl = D;
-    DD.DeprecationData.Message = Msg;
+    DD.DeprecationData.Message = Msg.data();
+    DD.DeprecationData.MessageLen = Msg.size();
     return DD;
   }
 
@@ -157,11 +150,37 @@
   }
 
   AccessedEntity &getAccessData() {
+    assert(Kind == Access && "Not an access diagnostic.");
     return *reinterpret_cast<AccessedEntity*>(AccessData);
   }
   const AccessedEntity &getAccessData() const {
+    assert(Kind == Access && "Not an access diagnostic.");
     return *reinterpret_cast<const AccessedEntity*>(AccessData);
   }
+
+  const NamedDecl *getDeprecationDecl() const {
+    assert(Kind == Deprecation && "Not a deprecation diagnostic.");
+    return DeprecationData.Decl;
+  }
+
+  llvm::StringRef getDeprecationMessage() const {
+    assert(Kind == Deprecation && "Not a deprecation diagnostic.");
+    return llvm::StringRef(DeprecationData.Message,
+                           DeprecationData.MessageLen);
+  }
+
+private:
+  union {
+    /// Deprecation.
+    struct {
+      const NamedDecl *Decl;
+      const char *Message;
+      size_t MessageLen;
+    } DeprecationData;
+
+    /// Access control.
+    char AccessData[sizeof(AccessedEntity)];
+  };
 };
 
 }

Modified: cfe/trunk/include/clang/Sema/Sema.h
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/include/clang/Sema/Sema.h?rev=116153&r1=116152&r2=116153&view=diff
==============================================================================
--- cfe/trunk/include/clang/Sema/Sema.h (original)
+++ cfe/trunk/include/clang/Sema/Sema.h Sat Oct  9 10:49:00 2010
@@ -1650,7 +1650,7 @@
 
   ParsingDeclStackState PushParsingDeclaration();
   void PopParsingDeclaration(ParsingDeclStackState S, Decl *D);
-  void EmitDeprecationWarning(NamedDecl *D, const char *Message,
+  void EmitDeprecationWarning(NamedDecl *D, llvm::StringRef Message,
                               SourceLocation Loc);
 
   void HandleDelayedDeprecationCheck(sema::DelayedDiagnostic &DD, Decl *Ctx);

Modified: cfe/trunk/lib/Sema/SemaDeclAttr.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Sema/SemaDeclAttr.cpp?rev=116153&r1=116152&r2=116153&view=diff
==============================================================================
--- cfe/trunk/lib/Sema/SemaDeclAttr.cpp (original)
+++ cfe/trunk/lib/Sema/SemaDeclAttr.cpp Sat Oct  9 10:49:00 2010
@@ -2566,16 +2566,16 @@
     return;
 
   DD.Triggered = true;
-  if (DD.DeprecationData.Message)
+  if (!DD.getDeprecationMessage().empty())
     Diag(DD.Loc, diag::warn_deprecated_message)
-      << DD.DeprecationData.Decl->getDeclName() 
-      << DD.DeprecationData.Message;
+      << DD.getDeprecationDecl()->getDeclName()
+      << DD.getDeprecationMessage();
   else
     Diag(DD.Loc, diag::warn_deprecated)
-      << DD.DeprecationData.Decl->getDeclName();
+      << DD.getDeprecationDecl()->getDeclName();
 }
 
-void Sema::EmitDeprecationWarning(NamedDecl *D, const char * Message,
+void Sema::EmitDeprecationWarning(NamedDecl *D, llvm::StringRef Message,
                                   SourceLocation Loc) {
   // Delay if we're currently parsing a declaration.
   if (ParsingDeclDepth) {
@@ -2587,7 +2587,7 @@
   // Otherwise, don't warn if our current context is deprecated.
   if (isDeclDeprecated(cast<Decl>(CurContext)))
     return;
-  if (Message)
+  if (!Message.empty())
     Diag(Loc, diag::warn_deprecated_message) << D->getDeclName() 
                                              << Message;
   else

Modified: cfe/trunk/lib/Sema/SemaExpr.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Sema/SemaExpr.cpp?rev=116153&r1=116152&r2=116153&view=diff
==============================================================================
--- cfe/trunk/lib/Sema/SemaExpr.cpp (original)
+++ cfe/trunk/lib/Sema/SemaExpr.cpp Sat Oct  9 10:49:00 2010
@@ -57,11 +57,8 @@
 ///
 bool Sema::DiagnoseUseOfDecl(NamedDecl *D, SourceLocation Loc) {
   // See if the decl is deprecated.
-  if (const DeprecatedAttr *DA = D->getAttr<DeprecatedAttr>()) {
-    const char *Message = 
-      DA->getMessage().empty() ? 0 : DA->getMessage().data();
-    EmitDeprecationWarning(D, Message, Loc);
-  }
+  if (const DeprecatedAttr *DA = D->getAttr<DeprecatedAttr>())
+    EmitDeprecationWarning(D, DA->getMessage(), Loc);
 
   // See if the decl is unavailable
   if (const UnavailableAttr *UA = D->getAttr<UnavailableAttr>()) {
@@ -69,7 +66,7 @@
       Diag(Loc, diag::err_unavailable) << D->getDeclName();
     else
       Diag(Loc, diag::err_unavailable_message) 
-        << D->getDeclName() << UA->getMessage().data();
+        << D->getDeclName() << UA->getMessage();
     Diag(D->getLocation(), diag::note_unavailable_here) << 0;
   }
 





More information about the cfe-commits mailing list