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

Chris Lattner sabre at nondot.org
Sat Jan 12 10:39:25 PST 2008


Author: lattner
Date: Sat Jan 12 12:39:25 2008
New Revision: 45910

URL: http://llvm.org/viewvc/llvm-project?rev=45910&view=rev
Log:
Tighten up handling of __func__ and friends: it should be an array
of const char, and it should error if it occurs outside a function.

Is it valid in an objc method?  If so we should handle that too.


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

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

==============================================================================
--- cfe/trunk/Sema/SemaExpr.cpp (original)
+++ cfe/trunk/Sema/SemaExpr.cpp Sat Jan 12 12:39:25 2008
@@ -115,25 +115,23 @@
   PreDefinedExpr::IdentType IT;
   
   switch (Kind) {
-  default:
-    assert(0 && "Unknown simple primary expr!");
-  case tok::kw___func__:       // primary-expression: __func__ [C99 6.4.2.2]
-    IT = PreDefinedExpr::Func;
-    break;
-  case tok::kw___FUNCTION__:   // primary-expression: __FUNCTION__ [GNU]
-    IT = PreDefinedExpr::Function;
-    break;
-  case tok::kw___PRETTY_FUNCTION__:  // primary-expression: __P..Y_F..N__ [GNU]
-    IT = PreDefinedExpr::PrettyFunction;
-    break;
+  default: assert(0 && "Unknown simple primary expr!");
+  case tok::kw___func__: IT = PreDefinedExpr::Func; break; // [C99 6.4.2.2]
+  case tok::kw___FUNCTION__: IT = PreDefinedExpr::Function; break;
+  case tok::kw___PRETTY_FUNCTION__: IT = PreDefinedExpr::PrettyFunction; break;
   }
+
+  // Verify that this is in a function context.
+  if (CurFunctionDecl == 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.
   llvm::APSInt Length(32);
   Length = CurFunctionDecl->getIdentifier()->getLength() + 1;
-  QualType ResTy = Context.getConstantArrayType(Context.CharTy, Length,
-                                                ArrayType::Normal, 0);
+  
+  QualType ResTy = Context.CharTy.getQualifiedType(QualType::Const);
+  ResTy = Context.getConstantArrayType(ResTy, Length, ArrayType::Normal, 0);
   return new PreDefinedExpr(Loc, ResTy, IT);
 }
 

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

==============================================================================
--- cfe/trunk/include/clang/Basic/DiagnosticKinds.def (original)
+++ cfe/trunk/include/clang/Basic/DiagnosticKinds.def Sat Jan 12 12:39:25 2008
@@ -501,6 +501,9 @@
 
 DIAG(err_hex_escape_no_digits, ERROR,
      "\\x used with no following hex digits")
+     
+DIAG(err_predef_outside_function, ERROR,
+     "predefined identifier is only valid inside function")
 
 // Declarations.
 DIAG(err_typename_requires_specqual, ERROR,

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

==============================================================================
--- cfe/trunk/test/Sema/predef.c (original)
+++ cfe/trunk/test/Sema/predef.c Sat Jan 12 12:39:25 2008
@@ -1,7 +1,12 @@
-// RUN: clang -fsyntax-only %s
+// RUN: clang -fsyntax-only -verify %s
 
-int abcdefghi12(void) {
+void abcdefghi12(void) {
  const char (*ss)[12] = &__func__;
- return sizeof(__func__);
+ static int arr[sizeof(__func__)==12 ? 1 : -1];
 }
 
+char *X = __func__; // expected-error {{predefined identifier is only valid}}
+
+void a() {
+  __func__[0] = 'a';  // expected-error {{variable is not assignable}}
+}





More information about the cfe-commits mailing list