Add Parser support for #pragma pack
 - Uses Action::ActOnPragmaPack
 - Test case is XFAIL pending verifier fixes.


git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@57066 91177308-0d34-0410-b5e6-96231b3b80d8
diff --git a/lib/Parse/ParsePragma.cpp b/lib/Parse/ParsePragma.cpp
new file mode 100644
index 0000000..fbdb123
--- /dev/null
+++ b/lib/Parse/ParsePragma.cpp
@@ -0,0 +1,108 @@
+//===--- ParsePragma.cpp - Language specific pragma parsing ---------------===//
+//
+//                     The LLVM Compiler Infrastructure
+//
+// This file is distributed under the University of Illinois Open Source
+// License. See LICENSE.TXT for details.
+//
+//===----------------------------------------------------------------------===//
+//
+// This file implements the language specific #pragma handlers.
+//
+//===----------------------------------------------------------------------===//
+
+#include "ParsePragma.h"
+#include "clang/Basic/Diagnostic.h"
+#include "clang/Lex/Preprocessor.h"
+#include "clang/Parse/Action.h"
+using namespace clang;
+
+// #pragma pack(...) comes in the following delicious flavors:
+//   pack '(' [integer] ')'
+//   pack '(' 'show' ')'
+//   pack '(' ('push' | 'pop') [',' identifier] [, integer] ')'
+void PragmaPackHandler::HandlePragma(Preprocessor &PP, Token &PackTok) {
+  // FIXME: Should we be expanding macros here? My guess is no.
+  SourceLocation PackLoc = PackTok.getLocation();
+
+  Token Tok;
+  PP.Lex(Tok);
+  if (Tok.isNot(tok::l_paren)) {
+    PP.Diag(Tok.getLocation(), diag::warn_pragma_pack_expected_lparen);
+    return;
+  }
+
+  Action::PragmaPackKind Kind = Action::PPK_Default;
+  IdentifierInfo *Name = 0;
+  Action::ExprResult Alignment;
+  SourceLocation LParenLoc = Tok.getLocation();
+  PP.Lex(Tok);  
+  if (Tok.is(tok::numeric_constant)) {
+    Alignment = Actions.ActOnNumericConstant(Tok);
+    if (Alignment.isInvalid)
+      return;
+
+    PP.Lex(Tok);
+  } else if (Tok.is(tok::identifier)) {
+    const IdentifierInfo *II = Tok.getIdentifierInfo();
+    if (II == &PP.getIdentifierTable().get("show")) {
+      Kind = Action::PPK_Show;
+      PP.Lex(Tok);
+    } else {
+      if (II == &PP.getIdentifierTable().get("push")) {
+        Kind = Action::PPK_Push;
+      } else if (II == &PP.getIdentifierTable().get("pop")) {
+        Kind = Action::PPK_Pop;
+      } else {
+        PP.Diag(Tok.getLocation(), diag::warn_pragma_pack_invalid_action);
+        return;
+      }        
+      PP.Lex(Tok);
+    
+      if (Tok.is(tok::comma)) {
+        PP.Lex(Tok);
+        
+        if (Tok.is(tok::numeric_constant)) {
+          Alignment = Actions.ActOnNumericConstant(Tok);
+          if (Alignment.isInvalid)
+            return;
+
+          PP.Lex(Tok);
+        } else if (Tok.is(tok::identifier)) {
+          Name = Tok.getIdentifierInfo();
+          PP.Lex(Tok);
+          
+          if (Tok.is(tok::comma)) {
+            PP.Lex(Tok);
+            
+            if (Tok.isNot(tok::numeric_constant)) {
+              PP.Diag(Tok.getLocation(), diag::warn_pragma_pack_malformed, 
+                      II->getName());
+              return;
+            }
+            
+            Alignment = Actions.ActOnNumericConstant(Tok);
+            if (Alignment.isInvalid)
+              return;
+
+            PP.Lex(Tok);
+          }
+        } else {
+          PP.Diag(Tok.getLocation(), diag::warn_pragma_pack_malformed, 
+                  II->getName());
+          return;
+        }
+      }
+    }
+  } 
+
+  if (Tok.isNot(tok::r_paren)) {
+    PP.Diag(Tok.getLocation(), diag::warn_pragma_pack_expected_rparen);
+    return;
+  }
+
+  SourceLocation RParenLoc = Tok.getLocation();
+  Actions.ActOnPragmaPack(Kind, Name, Alignment.Val, PackLoc, 
+                          LParenLoc, RParenLoc);
+}
+
diff --git a/lib/Parse/ParsePragma.h b/lib/Parse/ParsePragma.h
new file mode 100644
index 0000000..8a9ae57
--- /dev/null
+++ b/lib/Parse/ParsePragma.h
@@ -0,0 +1,33 @@
+//===---- ParserPragmas.h - Language specific pragmas -----------*- C++ -*-===//
+//
+//                     The LLVM Compiler Infrastructure
+//
+// This file is distributed under the University of Illinois Open Source
+// License. See LICENSE.TXT for details.
+//
+//===----------------------------------------------------------------------===//
+//
+// This file defines #pragma handlers for language specific pragmas.
+//
+//===----------------------------------------------------------------------===//
+
+#ifndef LLVM_CLANG_PARSE_PARSEPRAGMA_H
+#define LLVM_CLANG_PARSE_PARSEPRAGMA_H
+
+#include "clang/Lex/Pragma.h"
+
+namespace clang {
+  class Action;
+
+class PragmaPackHandler : public PragmaHandler {
+  Action &Actions;
+public:
+  PragmaPackHandler(const IdentifierInfo *N, Action &A) : PragmaHandler(N), 
+                                                          Actions(A) {}
+  
+  virtual void HandlePragma(Preprocessor &PP, Token &FirstToken);  
+};
+
+}  // end namespace clang
+
+#endif
diff --git a/lib/Parse/Parser.cpp b/lib/Parse/Parser.cpp
index 797de92..800be8d 100644
--- a/lib/Parse/Parser.cpp
+++ b/lib/Parse/Parser.cpp
@@ -15,6 +15,7 @@
 #include "clang/Basic/Diagnostic.h"
 #include "clang/Parse/DeclSpec.h"
 #include "clang/Parse/Scope.h"
+#include "ParsePragma.h"
 using namespace clang;
 
 Parser::Parser(Preprocessor &pp, Action &actions)
@@ -24,6 +25,13 @@
   NumCachedScopes = 0;
   ParenCount = BracketCount = BraceCount = 0;
   ObjCImpDecl = 0;
+
+  // Add #pragma handlers. These are removed and destroyed in the
+  // destructor.
+  PackHandler =
+    new PragmaPackHandler(&PP.getIdentifierTable().get("pack"), actions);
+  PP.AddPragmaHandler(0, PackHandler);
+
   // Instantiate a LexedMethodsForTopClass for all the non-nested classes.
   PushTopClassStack();
 }
@@ -233,6 +241,10 @@
   // Free the scope cache.
   for (unsigned i = 0, e = NumCachedScopes; i != e; ++i)
     delete ScopeCache[i];
+
+  // Remove the pragma handlers we installed.
+  PP.RemovePragmaHandler(0, PackHandler);
+  delete PackHandler;
 }
 
 /// Initialize - Warm up the parser.