aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--include/clang/Basic/DiagnosticLexKinds.td4
-rw-r--r--lib/Lex/PPDirectives.cpp20
-rw-r--r--test/Preprocessor/macro_arg_directive.c20
-rw-r--r--test/Preprocessor/macro_arg_directive.h9
4 files changed, 50 insertions, 3 deletions
diff --git a/include/clang/Basic/DiagnosticLexKinds.td b/include/clang/Basic/DiagnosticLexKinds.td
index deb6af88b3..cc202b48e5 100644
--- a/include/clang/Basic/DiagnosticLexKinds.td
+++ b/include/clang/Basic/DiagnosticLexKinds.td
@@ -202,8 +202,10 @@ def warn_cxx98_compat_variadic_macro : Warning<
InGroup<CXX98CompatPedantic>, DefaultIgnore;
def ext_named_variadic_macro : Extension<
"named variadic macros are a GNU extension">, InGroup<VariadicMacros>;
+def err_embedded_include : Error<
+ "embedding a #%0 directive within macro arguments is not supported">;
def ext_embedded_directive : Extension<
- "embedding a directive within macro arguments is not portable">,
+ "embedding a directive within macro arguments has undefined behavior">,
InGroup<DiagGroup<"embedded-directive">>;
def ext_missing_varargs_arg : Extension<
"varargs argument missing, but tolerated as an extension">;
diff --git a/lib/Lex/PPDirectives.cpp b/lib/Lex/PPDirectives.cpp
index ba65c29e69..7c8bfd7e7c 100644
--- a/lib/Lex/PPDirectives.cpp
+++ b/lib/Lex/PPDirectives.cpp
@@ -577,9 +577,25 @@ void Preprocessor::HandleDirective(Token &Result) {
// A(abc
// #warning blah
// def)
- // If so, the user is relying on non-portable behavior, emit a diagnostic.
- if (InMacroArgs)
+ // If so, the user is relying on undefined behavior, emit a diagnostic. Do
+ // not support this for #include-like directives, since that can result in
+ // terrible diagnostics, and does not work in GCC.
+ if (InMacroArgs) {
+ if (IdentifierInfo *II = Result.getIdentifierInfo()) {
+ switch (II->getPPKeywordID()) {
+ case tok::pp_include:
+ case tok::pp_import:
+ case tok::pp_include_next:
+ case tok::pp___include_macros:
+ Diag(Result, diag::err_embedded_include) << II->getName();
+ DiscardUntilEndOfDirective();
+ return;
+ default:
+ break;
+ }
+ }
Diag(Result, diag::ext_embedded_directive);
+ }
TryAgain:
switch (Result.getKind()) {
diff --git a/test/Preprocessor/macro_arg_directive.c b/test/Preprocessor/macro_arg_directive.c
new file mode 100644
index 0000000000..5c9943d605
--- /dev/null
+++ b/test/Preprocessor/macro_arg_directive.c
@@ -0,0 +1,20 @@
+// RUN: %clang_cc1 %s -fsyntax-only -verify
+
+// header1.h
+void fail(const char *);
+#define MUNCH(...) \
+ ({ int result = 0; __VA_ARGS__; if (!result) { fail(#__VA_ARGS__); }; result })
+
+static inline int f(int k) {
+ return MUNCH( // expected-error {{expected ')'}} expected-note {{to match this '('}} expected-error {{returning 'void'}}
+ if (k < 3)
+ result = 24;
+ else if (k > 4)
+ result = k - 4;
+}
+
+#include "macro_arg_directive.h" // expected-error {{embedding a #include directive within macro arguments is not supported}}
+
+int g(int k) {
+ return f(k) + f(k-1));
+}
diff --git a/test/Preprocessor/macro_arg_directive.h b/test/Preprocessor/macro_arg_directive.h
new file mode 100644
index 0000000000..892dbf2b13
--- /dev/null
+++ b/test/Preprocessor/macro_arg_directive.h
@@ -0,0 +1,9 @@
+// Support header for macro_arg_directive.c
+
+int n;
+
+struct S {
+ int k;
+};
+
+void g(int);