[cfe-commits] r60921 - in /cfe/trunk: include/clang/Basic/DiagnosticKinds.def lib/Sema/SemaExpr.cpp test/Sema/predef.c

Chris Lattner sabre at nondot.org
Thu Dec 11 21:05:22 PST 2008


Author: lattner
Date: Thu Dec 11 23:05:20 2008
New Revision: 60921

URL: http://llvm.org/viewvc/llvm-project?rev=60921&view=rev
Log:
fix rdar://6097892 - gcc incompat: clang rejects __func__, __FUNCTION__, and __PRETTY_FUNCTION__ outside func

Yeah, this is "useful".

Modified:
    cfe/trunk/include/clang/Basic/DiagnosticKinds.def
    cfe/trunk/lib/Sema/SemaExpr.cpp
    cfe/trunk/test/Sema/predef.c

Modified: cfe/trunk/include/clang/Basic/DiagnosticKinds.def
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/include/clang/Basic/DiagnosticKinds.def?rev=60921&r1=60920&r2=60921&view=diff

==============================================================================
--- cfe/trunk/include/clang/Basic/DiagnosticKinds.def (original)
+++ cfe/trunk/include/clang/Basic/DiagnosticKinds.def Thu Dec 11 23:05:20 2008
@@ -676,7 +676,7 @@
 DIAG(err_hex_escape_no_digits, ERROR,
      "\\x used with no following hex digits")
      
-DIAG(err_predef_outside_function, ERROR,
+DIAG(ext_predef_outside_function, WARNING,
      "predefined identifier is only valid inside function")
 
 // Declarations.

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

==============================================================================
--- cfe/trunk/lib/Sema/SemaExpr.cpp (original)
+++ cfe/trunk/lib/Sema/SemaExpr.cpp Thu Dec 11 23:05:20 2008
@@ -597,17 +597,19 @@
   case tok::kw___PRETTY_FUNCTION__: IT = PredefinedExpr::PrettyFunction; break;
   }
 
-  // Verify that this is in a function context.
-  if (getCurFunctionOrMethodDecl() == 0)
-    return Diag(Loc, diag::err_predef_outside_function);
-  
   // Pre-defined identifiers are of type char[x], where x is the length of the
   // string.
   unsigned Length;
   if (FunctionDecl *FD = getCurFunctionDecl())
     Length = FD->getIdentifier()->getLength();
-  else
-    Length = getCurMethodDecl()->getSynthesizedMethodSize();
+  else if (ObjCMethodDecl *MD = getCurMethodDecl())
+    Length = MD->getSynthesizedMethodSize();
+  else {
+    Diag(Loc, diag::ext_predef_outside_function);
+    // __PRETTY_FUNCTION__ -> "top level", the others produce an empty string.
+    Length = IT == PredefinedExpr::PrettyFunction ? strlen("top level") : 0;
+  }
+  
   
   llvm::APInt LengthI(32, Length + 1);
   QualType ResTy = Context.CharTy.getQualifiedType(QualType::Const);

Modified: cfe/trunk/test/Sema/predef.c
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/test/Sema/predef.c?rev=60921&r1=60920&r2=60921&view=diff

==============================================================================
--- cfe/trunk/test/Sema/predef.c (original)
+++ cfe/trunk/test/Sema/predef.c Thu Dec 11 23:05:20 2008
@@ -5,8 +5,15 @@
  static int arr[sizeof(__func__)==12 ? 1 : -1];
 }
 
-char *X = __func__; // expected-error {{predefined identifier is only valid}}
+char *X = __func__; // expected-warning {{predefined identifier is only valid}} \
+                       expected-warning {{initializing 'char const [1]' discards qualifiers, expected 'char *'}}
 
 void a() {
   __func__[0] = 'a';  // expected-error {{variable is not assignable}}
 }
+
+// rdar://6097892 - GCC permits this insanity.
+const char *b = __func__;  // expected-warning {{predefined identifier is only valid}}
+const char *c = __FUNCTION__; // expected-warning {{predefined identifier is only valid}}
+const char *d = __PRETTY_FUNCTION__; // expected-warning {{predefined identifier is only valid}}
+





More information about the cfe-commits mailing list