[cfe-commits] r149014 - in /cfe/trunk: include/clang/Basic/TokenKinds.def include/clang/Parse/Parser.h lib/Parse/ParsePragma.cpp lib/Parse/Parser.cpp test/CodeGenCXX/pr11797.cpp test/CodeGenCXX/pragma-visibility.cpp

Rafael Espindola rafael.espindola at gmail.com
Wed Jan 25 18:02:57 PST 2012


Author: rafael
Date: Wed Jan 25 20:02:57 2012
New Revision: 149014

URL: http://llvm.org/viewvc/llvm-project?rev=149014&view=rev
Log:
Fix our handling of #pragma GCC visibility.
Now the lexer just produces a token and the parser is the one responsible for
activating it.
This fixes problem like the one pr11797 where the lexer and the parser were not
in sync. This also let us be more strict on where in the file we accept
these pragmas.

Added:
    cfe/trunk/test/CodeGenCXX/pr11797.cpp
Modified:
    cfe/trunk/include/clang/Basic/TokenKinds.def
    cfe/trunk/include/clang/Parse/Parser.h
    cfe/trunk/lib/Parse/ParsePragma.cpp
    cfe/trunk/lib/Parse/Parser.cpp
    cfe/trunk/test/CodeGenCXX/pragma-visibility.cpp

Modified: cfe/trunk/include/clang/Basic/TokenKinds.def
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/include/clang/Basic/TokenKinds.def?rev=149014&r1=149013&r2=149014&view=diff
==============================================================================
--- cfe/trunk/include/clang/Basic/TokenKinds.def (original)
+++ cfe/trunk/include/clang/Basic/TokenKinds.def Wed Jan 25 20:02:57 2012
@@ -568,6 +568,11 @@
 // one 'pragma_unused' annotation token followed by the argument token.
 ANNOTATION(pragma_unused)
 
+// Annotation for #pragma GCC visibility...
+// The lexer produces these so that they only take effect when the parser
+// handles them.
+ANNOTATION(pragma_vis)
+
 #undef ANNOTATION
 #undef TESTING_KEYWORD
 #undef OBJC2_AT_KEYWORD

Modified: cfe/trunk/include/clang/Parse/Parser.h
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/include/clang/Parse/Parser.h?rev=149014&r1=149013&r2=149014&view=diff
==============================================================================
--- cfe/trunk/include/clang/Parse/Parser.h (original)
+++ cfe/trunk/include/clang/Parse/Parser.h Wed Jan 25 20:02:57 2012
@@ -410,6 +410,10 @@
   /// \brief Handle the annotation token produced for #pragma unused(...)
   void HandlePragmaUnused();
 
+  /// \brief Handle the annotation token produced for
+  /// #pragma GCC visibility...
+  void HandlePragmaVisibility();
+
   /// GetLookAheadToken - This peeks ahead N tokens and returns that token
   /// without consuming any tokens.  LookAhead(0) returns 'Tok', LookAhead(1)
   /// returns the token after Tok, etc.

Modified: cfe/trunk/lib/Parse/ParsePragma.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Parse/ParsePragma.cpp?rev=149014&r1=149013&r2=149014&view=diff
==============================================================================
--- cfe/trunk/lib/Parse/ParsePragma.cpp (original)
+++ cfe/trunk/lib/Parse/ParsePragma.cpp Wed Jan 25 20:02:57 2012
@@ -29,6 +29,14 @@
   ConsumeToken(); // The argument token.
 }
 
+void Parser::HandlePragmaVisibility() {
+  assert(Tok.is(tok::annot_pragma_vis));
+  const IdentifierInfo *VisType =
+    static_cast<IdentifierInfo *>(Tok.getAnnotationValue());
+  SourceLocation VisLoc = ConsumeToken();
+  Actions.ActOnPragmaVisibility(VisType, VisLoc);
+}
+
 // #pragma GCC visibility comes in two variants:
 //   'push' '(' [visibility] ')'
 //   'pop'
@@ -77,7 +85,14 @@
     return;
   }
 
-  Actions.ActOnPragmaVisibility(VisType, VisLoc);
+  Token *Toks = new Token[1];
+  Toks[0].startToken();
+  Toks[0].setKind(tok::annot_pragma_vis);
+  Toks[0].setLocation(VisLoc);
+  Toks[0].setAnnotationValue(
+                          const_cast<void*>(static_cast<const void*>(VisType)));
+  PP.EnterTokenStream(Toks, 1, /*DisableMacroExpansion=*/true,
+                      /*OwnsTokens=*/true);
 }
 
 // #pragma pack(...) comes in the following delicious flavors:

Modified: cfe/trunk/lib/Parse/Parser.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Parse/Parser.cpp?rev=149014&r1=149013&r2=149014&view=diff
==============================================================================
--- cfe/trunk/lib/Parse/Parser.cpp (original)
+++ cfe/trunk/lib/Parse/Parser.cpp Wed Jan 25 20:02:57 2012
@@ -543,6 +543,9 @@
 
   Decl *SingleDecl = 0;
   switch (Tok.getKind()) {
+  case tok::annot_pragma_vis:
+    HandlePragmaVisibility();
+    return DeclGroupPtrTy();
   case tok::semi:
     Diag(Tok, getLang().CPlusPlus0x ?
          diag::warn_cxx98_compat_top_level_semi : diag::ext_top_level_semi)

Added: cfe/trunk/test/CodeGenCXX/pr11797.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/test/CodeGenCXX/pr11797.cpp?rev=149014&view=auto
==============================================================================
--- cfe/trunk/test/CodeGenCXX/pr11797.cpp (added)
+++ cfe/trunk/test/CodeGenCXX/pr11797.cpp Wed Jan 25 20:02:57 2012
@@ -0,0 +1,8 @@
+// RUN: %clang_cc1 %s -fvisibility hidden -emit-llvm -o - | FileCheck %s
+
+namespace std __attribute__ ((__visibility__ ("default"))) {}
+#pragma GCC visibility push(default)
+void foo() {
+}
+#pragma GCC visibility pop
+// CHECK: define void @_Z3foov()

Modified: cfe/trunk/test/CodeGenCXX/pragma-visibility.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/test/CodeGenCXX/pragma-visibility.cpp?rev=149014&r1=149013&r2=149014&view=diff
==============================================================================
--- cfe/trunk/test/CodeGenCXX/pragma-visibility.cpp (original)
+++ cfe/trunk/test/CodeGenCXX/pragma-visibility.cpp Wed Jan 25 20:02:57 2012
@@ -55,8 +55,6 @@
 #pragma GCC visibility pop
 
 namespace n __attribute((visibility("default")))  {
-  extern int foofoo; // FIXME: Shouldn't be necessary, but otherwise the pragma
-                     //        gets to Sema before the namespace!
 #pragma GCC visibility push(hidden)
   void g() {}
   // CHECK: define hidden void @_ZN1n1gEv
@@ -66,8 +64,6 @@
 // We used to test this, but it's insane, so unless it happens in
 // headers, we should not support it.
 namespace n __attribute((visibility("hidden"))) {
-  extern int foofoo; // FIXME: Shouldn't be necessary, but otherwise the pragma
-                     //        gets to Sema before the namespace!
   #pragma GCC visibility pop
   void h() {}
   // CHECK disabled: define void @_ZN1n1hEv





More information about the cfe-commits mailing list