[PATCH] D125178: Warn if using `elifdef` & `elifndef` in not C2x mode
Ken Matsui via Phabricator via cfe-commits
cfe-commits at lists.llvm.org
Sat May 7 14:37:37 PDT 2022
ken-matsui created this revision.
ken-matsui added a reviewer: aaron.ballman.
Herald added a project: All.
ken-matsui requested review of this revision.
Herald added a project: clang.
Herald added a subscriber: cfe-commits.
This patch resolves https://github.com/llvm/llvm-project/issues/55306.
Repository:
rG LLVM Github Monorepo
https://reviews.llvm.org/D125178
Files:
clang/include/clang/Basic/DiagnosticLexKinds.td
clang/lib/Lex/PPDirectives.cpp
clang/test/Preprocessor/ext-c2x-pp-directive.c
Index: clang/test/Preprocessor/ext-c2x-pp-directive.c
===================================================================
--- /dev/null
+++ clang/test/Preprocessor/ext-c2x-pp-directive.c
@@ -0,0 +1,14 @@
+// RUN: %clang_cc1 -std=c99 -fsyntax-only -verify -pedantic %s
+// RUN: not %clang_cc1 -std=c99 -fsyntax-only -verify %s
+// RUN: not %clang_cc1 -std=c2x -fsyntax-only -verify -pedantic %s
+// RUN: not %clang_cc1 -std=c2x -fsyntax-only -verify %s
+
+#if 1
+#elifndef FOO // expected-warning {{#elifndef is a C2x extension}}
+#endif
+
+#if 1
+#elifdef BAR // expected-warning {{#elifdef is a C2x extension}}
+#endif
+
+// expected-warning {{ISO C requires a translation unit to contain at least one declaration}}
Index: clang/lib/Lex/PPDirectives.cpp
===================================================================
--- clang/lib/Lex/PPDirectives.cpp
+++ clang/lib/Lex/PPDirectives.cpp
@@ -3255,6 +3255,17 @@
: PED_Elifndef;
++NumElse;
+ // Warn if using `#elifdef` & `#elifndef` in not C2x mode.
+ switch (DirKind) {
+ case PED_Elifdef:
+ case PED_Elifndef:
+ if (!getLangOpts().C2x)
+ Diag(ElifToken, diag::ext_c2x_pp_directive) << DirKind - 1;
+ break;
+ default:
+ break;
+ }
+
// #elif directive in a non-skipping conditional... start skipping.
// We don't care what the condition is, because we will always skip it (since
// the block immediately before it was included).
Index: clang/include/clang/Basic/DiagnosticLexKinds.td
===================================================================
--- clang/include/clang/Basic/DiagnosticLexKinds.td
+++ clang/include/clang/Basic/DiagnosticLexKinds.td
@@ -693,6 +693,9 @@
def warn_cxx98_compat_pp_line_too_big : Warning<
"#line number greater than 32767 is incompatible with C++98">,
InGroup<CXX98CompatPedantic>, DefaultIgnore;
+def ext_c2x_pp_directive : Extension<
+ "%select{#elifdef|#elifndef}0 is a C2x extension">,
+ InGroup<CPre2xCompatPedantic>;
def err_pp_visibility_non_macro : Error<"no macro named %0">;
-------------- next part --------------
A non-text attachment was scrubbed...
Name: D125178.427898.patch
Type: text/x-patch
Size: 2089 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20220507/6558eaa4/attachment-0001.bin>
More information about the cfe-commits
mailing list