aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--include/clang/Basic/DiagnosticKinds.def2
-rw-r--r--lib/Sema/SemaExpr.cpp14
-rw-r--r--test/Sema/predef.c9
3 files changed, 17 insertions, 8 deletions
diff --git a/include/clang/Basic/DiagnosticKinds.def b/include/clang/Basic/DiagnosticKinds.def
index 807d32853d..a70f70eb03 100644
--- a/include/clang/Basic/DiagnosticKinds.def
+++ b/include/clang/Basic/DiagnosticKinds.def
@@ -676,7 +676,7 @@ DIAG(warn_octal_escape_too_large, WARNING,
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.
diff --git a/lib/Sema/SemaExpr.cpp b/lib/Sema/SemaExpr.cpp
index ba5d28a633..264b85b69c 100644
--- a/lib/Sema/SemaExpr.cpp
+++ b/lib/Sema/SemaExpr.cpp
@@ -597,17 +597,19 @@ Sema::ExprResult Sema::ActOnPredefinedExpr(SourceLocation Loc,
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);
diff --git a/test/Sema/predef.c b/test/Sema/predef.c
index bd5e1a98b2..097950ccc6 100644
--- a/test/Sema/predef.c
+++ b/test/Sema/predef.c
@@ -5,8 +5,15 @@ void abcdefghi12(void) {
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}}
+