r209963 - Preprocessor: recover gracefully when C++ operator names are used as macro identifiers

Alp Toker alp at nuanti.com
Fri May 30 20:38:17 PDT 2014


Author: alp
Date: Fri May 30 22:38:17 2014
New Revision: 209963

URL: http://llvm.org/viewvc/llvm-project?rev=209963&view=rev
Log:
Preprocessor: recover gracefully when C++ operator names are used as macro identifiers

This failure mode shows up occasionally when users try to include C headers in
C++ projects or when porting from Windows. We might as well recover in the way
the user expected, thus avoiding confusing diagnostic messages at point of use.

Modified:
    cfe/trunk/include/clang/Basic/DiagnosticLexKinds.td
    cfe/trunk/lib/Lex/PPDirectives.cpp
    cfe/trunk/test/Preprocessor/cxx_oper_keyword.cpp

Modified: cfe/trunk/include/clang/Basic/DiagnosticLexKinds.td
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/include/clang/Basic/DiagnosticLexKinds.td?rev=209963&r1=209962&r2=209963&view=diff
==============================================================================
--- cfe/trunk/include/clang/Basic/DiagnosticLexKinds.td (original)
+++ cfe/trunk/include/clang/Basic/DiagnosticLexKinds.td Fri May 30 22:38:17 2014
@@ -500,7 +500,7 @@ def ext_pp_bad_paste_ms : ExtWarn<
   "pasting formed '%0', an invalid preprocessing token">, DefaultError,
   InGroup<DiagGroup<"invalid-token-paste">>;
 def err_pp_operator_used_as_macro_name : Error<
-  "C++ operator %0 (aka %1) cannot be used as a macro name">;
+  "C++ operator %0 (aka %1) used as a macro name">;
 def err_pp_illegal_floating_literal : Error<
   "floating point literal in preprocessor expression">;
 def err_pp_line_requires_integer : Error<

Modified: cfe/trunk/lib/Lex/PPDirectives.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Lex/PPDirectives.cpp?rev=209963&r1=209962&r2=209963&view=diff
==============================================================================
--- cfe/trunk/lib/Lex/PPDirectives.cpp (original)
+++ cfe/trunk/lib/Lex/PPDirectives.cpp Fri May 30 22:38:17 2014
@@ -148,10 +148,11 @@ bool Preprocessor::CheckMacroName(Token
     if (!getLangOpts().MSVCCompat)
       // C++ 2.5p2: Alternative tokens behave the same as its primary token
       // except for their spellings.
-      return Diag(MacroNameTok, diag::err_pp_operator_used_as_macro_name)
-             << II << MacroNameTok.getKind();
+      Diag(MacroNameTok, diag::err_pp_operator_used_as_macro_name)
+          << II << MacroNameTok.getKind();
 
-    // Allow #defining |and| and friends for Microsoft compatibility.
+    // Allow #defining |and| and friends for Microsoft compatibility or
+    // recovery when legacy C headers are included in C++.
     MacroNameTok.setIdentifierInfo(II);
   }
 

Modified: cfe/trunk/test/Preprocessor/cxx_oper_keyword.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/test/Preprocessor/cxx_oper_keyword.cpp?rev=209963&r1=209962&r2=209963&view=diff
==============================================================================
--- cfe/trunk/test/Preprocessor/cxx_oper_keyword.cpp (original)
+++ cfe/trunk/test/Preprocessor/cxx_oper_keyword.cpp Fri May 30 22:38:17 2014
@@ -11,12 +11,21 @@
 // Not valid in C++ unless -fno-operator-names is passed:
 
 #ifdef OPERATOR_NAMES
-//expected-error at +2 {{C++ operator 'and' (aka '&&') cannot be used as a macro name}}
+//expected-error at +2 {{C++ operator 'and' (aka '&&') used as a macro name}}
 #endif
 #define and foo
 
 #ifdef OPERATOR_NAMES
-//expected-error at +2 {{C++ operator 'xor' (aka '^') cannot be used as a macro name}}
+//expected-error at +2 {{C++ operator 'xor' (aka '^') used as a macro name}}
 #endif
 #if defined xor
 #endif
+
+// For error recovery we continue as though the identifier was a macro name regardless of -fno-operator-names.
+#ifdef OPERATOR_NAMES
+//expected-error at +3 {{C++ operator 'and' (aka '&&') used as a macro name}}
+#endif
+//expected-warning at +2 {{and is defined}}
+#ifdef and
+#warning and is defined
+#endif





More information about the cfe-commits mailing list