[llvm-branch-commits] [cfe-branch] r197359 - Merging r196212:

Bill Wendling isanbard at gmail.com
Sun Dec 15 18:32:56 PST 2013


Author: void
Date: Sun Dec 15 20:32:55 2013
New Revision: 197359

URL: http://llvm.org/viewvc/llvm-project?rev=197359&view=rev
Log:
Merging r196212:
------------------------------------------------------------------------
r196212 | alp | 2013-12-02 22:13:01 -0800 (Mon, 02 Dec 2013) | 22 lines

Emit an extension warning when changing system header tokens

clang converts keywords to identifiers for compatibility with various system
headers such as GNU libc.

Implement a -Wkeyword-compat extension warning to diagnose those cases. The
warning is on by default but will generally be ignored in system headers. It
can however be enabled globally to aid standards conformance testing.

This also changes the __uptr keyword avoidance from r195710 to no longer
special-case system headers, bringing it in line with other similar workarounds
in clang.

Implementation returns bool for symmetry with token annotation functions.

Some examples:

warning: keyword '__is_pod' will be treated as an identifier for the remainder of the translation unit [-Wkeyword-compat]
struct __is_pod

warning: keyword '__uptr' will be treated as an identifier here [-Wkeyword-compat]
union w *__uptr;
------------------------------------------------------------------------

Modified:
    cfe/branches/release_34/   (props changed)
    cfe/branches/release_34/include/clang/Basic/DiagnosticGroups.td
    cfe/branches/release_34/include/clang/Basic/DiagnosticParseKinds.td
    cfe/branches/release_34/include/clang/Parse/Parser.h
    cfe/branches/release_34/lib/Parse/ParseDecl.cpp
    cfe/branches/release_34/lib/Parse/ParseDeclCXX.cpp
    cfe/branches/release_34/lib/Parse/Parser.cpp
    cfe/branches/release_34/test/PCH/cxx-traits.cpp
    cfe/branches/release_34/test/PCH/cxx-traits.h
    cfe/branches/release_34/test/Sema/Inputs/ms-keyword-system-header.h
    cfe/branches/release_34/test/Sema/ms-keyword-system-header.c

Propchange: cfe/branches/release_34/
------------------------------------------------------------------------------
--- svn:mergeinfo (original)
+++ svn:mergeinfo Sun Dec 15 20:32:55 2013
@@ -1,4 +1,4 @@
 /cfe/branches/type-system-rewrite:134693-134817
-/cfe/trunk:195126,195128,195135-195136,195146,195149,195154,195158,195163,195168,195174,195249,195268,195283,195303,195326,195329,195367,195384,195409,195420,195422,195495,195501,195547,195556,195558,195587,195620,195635,195669,195687,195693,195710,195713,195716,195756,195760,195768,195777,195789,195792,195804,195827,195843-195844,195877,195887-195888,195897,195903,195905-195906,195932,195936-195943,195970,195983,196045,196048,196050,196058,196114-196115,196143,196145,196153,196189-196192,196198-196199,196206,196208-196209,196211,196215,196359-196362,196370,196387,196423,196454,196456,196459,196488,196532-196533,196535,196538,196588,196593,196599,196612,196630,196658,196712,196720,196724,196771,196852,196957,196995,197022,197120,197298,197305
+/cfe/trunk:195126,195128,195135-195136,195146,195149,195154,195158,195163,195168,195174,195249,195268,195283,195303,195326,195329,195367,195384,195409,195420,195422,195495,195501,195547,195556,195558,195587,195620,195635,195669,195687,195693,195710,195713,195716,195756,195760,195768,195777,195789,195792,195804,195827,195843-195844,195877,195887-195888,195897,195903,195905-195906,195932,195936-195943,195970,195983,196045,196048,196050,196058,196114-196115,196143,196145,196153,196189-196192,196198-196199,196206,196208-196209,196211-196212,196215,196359-196362,196370,196387,196423,196454,196456,196459,196488,196532-196533,196535,196538,196588,196593,196599,196612,196630,196658,196712,196720,196724,196771,196852,196957,196995,197022,197120,197298,197305
 /cfe/trunk/test:170344
 /cfe/trunk/test/SemaTemplate:126920

Modified: cfe/branches/release_34/include/clang/Basic/DiagnosticGroups.td
URL: http://llvm.org/viewvc/llvm-project/cfe/branches/release_34/include/clang/Basic/DiagnosticGroups.td?rev=197359&r1=197358&r2=197359&view=diff
==============================================================================
--- cfe/branches/release_34/include/clang/Basic/DiagnosticGroups.td (original)
+++ cfe/branches/release_34/include/clang/Basic/DiagnosticGroups.td Sun Dec 15 20:32:55 2013
@@ -50,6 +50,7 @@ def BuiltinRequiresHeader : DiagGroup<"b
 def C99Compat : DiagGroup<"c99-compat">;
 def CXXCompat: DiagGroup<"c++-compat">;
 def ExternCCompat : DiagGroup<"extern-c-compat">;
+def KeywordCompat : DiagGroup<"keyword-compat">;
 def GNUCaseRange : DiagGroup<"gnu-case-range">;
 def CastAlign : DiagGroup<"cast-align">;
 def : DiagGroup<"cast-qual">;

Modified: cfe/branches/release_34/include/clang/Basic/DiagnosticParseKinds.td
URL: http://llvm.org/viewvc/llvm-project/cfe/branches/release_34/include/clang/Basic/DiagnosticParseKinds.td?rev=197359&r1=197358&r2=197359&view=diff
==============================================================================
--- cfe/branches/release_34/include/clang/Basic/DiagnosticParseKinds.td (original)
+++ cfe/branches/release_34/include/clang/Basic/DiagnosticParseKinds.td Sun Dec 15 20:32:55 2013
@@ -58,6 +58,9 @@ def ext_plain_complex : ExtWarn<
 def ext_integer_complex : Extension<
   "complex integer types are a GNU extension">, InGroup<GNUComplexInteger>;
 def ext_thread_before : Extension<"'__thread' before '%0'">;
+def ext_keyword_as_ident : ExtWarn<
+  "keyword '%0' will be treated as an identifier %select{here|for the remainder of the translation unit}1">,
+  InGroup<KeywordCompat>;
 
 def error_empty_enum : Error<"use of empty enum">;
 def err_invalid_sign_spec : Error<"'%0' cannot be signed or unsigned">;

Modified: cfe/branches/release_34/include/clang/Parse/Parser.h
URL: http://llvm.org/viewvc/llvm-project/cfe/branches/release_34/include/clang/Parse/Parser.h?rev=197359&r1=197358&r2=197359&view=diff
==============================================================================
--- cfe/branches/release_34/include/clang/Parse/Parser.h (original)
+++ cfe/branches/release_34/include/clang/Parse/Parser.h Sun Dec 15 20:32:55 2013
@@ -555,6 +555,13 @@ private:
                                 const char *&PrevSpec, unsigned &DiagID,
                                 bool &isInvalid);
 
+  /// TryKeywordIdentFallback - For compatibility with system headers using
+  /// keywords as identifiers, attempt to convert the current token to an
+  /// identifier and optionally disable the keyword for the remainder of the
+  /// translation unit. This returns false if the token was not replaced,
+  /// otherwise emits a diagnostic and returns true.
+  bool TryKeywordIdentFallback(bool DisableKeyword);
+
   /// \brief Get the TemplateIdAnnotation from the token.
   TemplateIdAnnotation *takeTemplateIdAnnotation(const Token &tok);
 

Modified: cfe/branches/release_34/lib/Parse/ParseDecl.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/branches/release_34/lib/Parse/ParseDecl.cpp?rev=197359&r1=197358&r2=197359&view=diff
==============================================================================
--- cfe/branches/release_34/lib/Parse/ParseDecl.cpp (original)
+++ cfe/branches/release_34/lib/Parse/ParseDecl.cpp Sun Dec 15 20:32:55 2013
@@ -2725,10 +2725,8 @@ void Parser::ParseDeclarationSpecifiers(
       // then treat __is_signed as an identifier rather than as a keyword.
       if (DS.getTypeSpecType() == TST_bool &&
           DS.getTypeQualifiers() == DeclSpec::TQ_const &&
-          DS.getStorageClassSpec() == DeclSpec::SCS_static) {
-        Tok.getIdentifierInfo()->RevertTokenIDToIdentifier();
-        Tok.setKind(tok::identifier);
-      }
+          DS.getStorageClassSpec() == DeclSpec::SCS_static)
+        TryKeywordIdentFallback(true);
 
       // We're done with the declaration-specifiers.
       goto DoneWithDeclSpec;
@@ -4474,10 +4472,9 @@ void Parser::ParseTypeQualifierListOpt(D
       // GNU libc headers in C mode use '__uptr' as an identifer which conflicts
       // with the MS modifier keyword.
       if (VendorAttributesAllowed && !getLangOpts().CPlusPlus &&
-          IdentifierRequired && DS.isEmpty() && NextToken().is(tok::semi) &&
-          PP.getSourceManager().isInSystemHeader(Loc)) {
-        Tok.setKind(tok::identifier);
-        continue;
+          IdentifierRequired && DS.isEmpty() && NextToken().is(tok::semi)) {
+        if (TryKeywordIdentFallback(false))
+          continue;
       }
     case tok::kw___sptr:
     case tok::kw___w64:

Modified: cfe/branches/release_34/lib/Parse/ParseDeclCXX.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/branches/release_34/lib/Parse/ParseDeclCXX.cpp?rev=197359&r1=197358&r2=197359&view=diff
==============================================================================
--- cfe/branches/release_34/lib/Parse/ParseDeclCXX.cpp (original)
+++ cfe/branches/release_34/lib/Parse/ParseDeclCXX.cpp Sun Dec 15 20:32:55 2013
@@ -1197,15 +1197,13 @@ void Parser::ParseClassSpecifier(tok::To
        Tok.is(tok::kw___is_scalar) ||
        Tok.is(tok::kw___is_signed) ||
        Tok.is(tok::kw___is_unsigned) ||
-       Tok.is(tok::kw___is_void))) {
+       Tok.is(tok::kw___is_void)))
     // GNU libstdc++ 4.2 and libc++ use certain intrinsic names as the
     // name of struct templates, but some are keywords in GCC >= 4.3
     // and Clang. Therefore, when we see the token sequence "struct
     // X", make X into a normal identifier rather than a keyword, to
     // allow libstdc++ 4.2 and libc++ to work properly.
-    Tok.getIdentifierInfo()->RevertTokenIDToIdentifier();
-    Tok.setKind(tok::identifier);
-  }
+    TryKeywordIdentFallback(true);
 
   // Parse the (optional) nested-name-specifier.
   CXXScopeSpec &SS = DS.getTypeSpecScope();

Modified: cfe/branches/release_34/lib/Parse/Parser.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/branches/release_34/lib/Parse/Parser.cpp?rev=197359&r1=197358&r2=197359&view=diff
==============================================================================
--- cfe/branches/release_34/lib/Parse/Parser.cpp (original)
+++ cfe/branches/release_34/lib/Parse/Parser.cpp Sun Dec 15 20:32:55 2013
@@ -1503,6 +1503,17 @@ Parser::TryAnnotateName(bool IsAddressOf
   return ANK_Unresolved;
 }
 
+bool Parser::TryKeywordIdentFallback(bool DisableKeyword) {
+  assert(Tok.isNot(tok::identifier));
+  Diag(Tok, diag::ext_keyword_as_ident)
+    << PP.getSpelling(Tok)
+    << DisableKeyword;
+  if (DisableKeyword)
+    Tok.getIdentifierInfo()->RevertTokenIDToIdentifier();
+  Tok.setKind(tok::identifier);
+  return true;
+}
+
 /// TryAnnotateTypeOrScopeToken - If the current token position is on a
 /// typename (possibly qualified in C++) or a C++ scope specifier not followed
 /// by a typename, TryAnnotateTypeOrScopeToken will replace one or more tokens

Modified: cfe/branches/release_34/test/PCH/cxx-traits.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/branches/release_34/test/PCH/cxx-traits.cpp?rev=197359&r1=197358&r2=197359&view=diff
==============================================================================
--- cfe/branches/release_34/test/PCH/cxx-traits.cpp (original)
+++ cfe/branches/release_34/test/PCH/cxx-traits.cpp Sun Dec 15 20:32:55 2013
@@ -2,9 +2,11 @@
 // RUN: %clang_cc1 -include %S/cxx-traits.h -std=c++11 -fsyntax-only -verify %s
 
 // RUN: %clang_cc1 -x c++-header -std=c++11 -emit-pch -o %t %S/cxx-traits.h
-// RUN: %clang_cc1 -std=c++11 -include-pch %t -fsyntax-only -verify %s
+// RUN: %clang_cc1 -std=c++11 -include-pch %t -DPCH -fsyntax-only -verify %s
 
+#ifdef PCH
 // expected-no-diagnostics
+#endif
 
 bool _Is_pod_comparator = __is_pod<int>::__value;
 bool _Is_empty_check = __is_empty<int>::__value;

Modified: cfe/branches/release_34/test/PCH/cxx-traits.h
URL: http://llvm.org/viewvc/llvm-project/cfe/branches/release_34/test/PCH/cxx-traits.h?rev=197359&r1=197358&r2=197359&view=diff
==============================================================================
--- cfe/branches/release_34/test/PCH/cxx-traits.h (original)
+++ cfe/branches/release_34/test/PCH/cxx-traits.h Sun Dec 15 20:32:55 2013
@@ -1,12 +1,12 @@
 // Header for PCH test cxx-traits.cpp
 
 template<typename _Tp>
-struct __is_pod {
+struct __is_pod { // expected-warning {{keyword '__is_pod' will be treated as an identifier for the remainder of the translation unit}}
   enum { __value };
 };
 
 template<typename _Tp>
-struct __is_empty {
+struct __is_empty { // expected-warning {{keyword '__is_empty' will be treated as an identifier for the remainder of the translation unit}}
   enum { __value };
 };
 

Modified: cfe/branches/release_34/test/Sema/Inputs/ms-keyword-system-header.h
URL: http://llvm.org/viewvc/llvm-project/cfe/branches/release_34/test/Sema/Inputs/ms-keyword-system-header.h?rev=197359&r1=197358&r2=197359&view=diff
==============================================================================
--- cfe/branches/release_34/test/Sema/Inputs/ms-keyword-system-header.h (original)
+++ cfe/branches/release_34/test/Sema/Inputs/ms-keyword-system-header.h Sun Dec 15 20:32:55 2013
@@ -2,5 +2,8 @@
 
 typedef union {
   union w *__uptr;
+#if defined(MS) && defined(NOT_SYSTEM)
+  // expected-warning at -2 {{keyword '__uptr' will be treated as an identifier here}}
+#endif
   int *__iptr;
 } WS __attribute__((__transparent_union__));

Modified: cfe/branches/release_34/test/Sema/ms-keyword-system-header.c
URL: http://llvm.org/viewvc/llvm-project/cfe/branches/release_34/test/Sema/ms-keyword-system-header.c?rev=197359&r1=197358&r2=197359&view=diff
==============================================================================
--- cfe/branches/release_34/test/Sema/ms-keyword-system-header.c (original)
+++ cfe/branches/release_34/test/Sema/ms-keyword-system-header.c Sun Dec 15 20:32:55 2013
@@ -1,4 +1,6 @@
 // RUN: %clang_cc1 -fms-extensions -D MS -isystem %S/Inputs %s -fsyntax-only -verify
+// RUN: %clang_cc1 -fms-extensions -D MS -Wno-keyword-compat -I %S/Inputs %s -fsyntax-only -verify
+// RUN: %clang_cc1 -fms-extensions -D MS -D NOT_SYSTEM -I %S/Inputs %s -fsyntax-only -verify
 // RUN: %clang_cc1 -isystem %S/Inputs %s -fsyntax-only -verify
 
 // PR17824: GNU libc uses MS keyword __uptr as an identifier in C mode





More information about the llvm-branch-commits mailing list