Daniel Dunbar | fcdd8fe | 2008-10-04 19:21:03 +0000 | [diff] [blame] | 1 | //===--- ParsePragma.cpp - Language specific pragma parsing ---------------===// |
| 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
| 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
| 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | // |
| 10 | // This file implements the language specific #pragma handlers. |
| 11 | // |
| 12 | //===----------------------------------------------------------------------===// |
| 13 | |
| 14 | #include "ParsePragma.h" |
Sebastian Redl | a55e52c | 2008-11-25 22:21:31 +0000 | [diff] [blame^] | 15 | #include "AstGuard.h" |
Daniel Dunbar | fcdd8fe | 2008-10-04 19:21:03 +0000 | [diff] [blame] | 16 | #include "clang/Basic/Diagnostic.h" |
| 17 | #include "clang/Lex/Preprocessor.h" |
| 18 | #include "clang/Parse/Action.h" |
| 19 | using namespace clang; |
| 20 | |
| 21 | // #pragma pack(...) comes in the following delicious flavors: |
| 22 | // pack '(' [integer] ')' |
| 23 | // pack '(' 'show' ')' |
| 24 | // pack '(' ('push' | 'pop') [',' identifier] [, integer] ')' |
| 25 | void PragmaPackHandler::HandlePragma(Preprocessor &PP, Token &PackTok) { |
| 26 | // FIXME: Should we be expanding macros here? My guess is no. |
| 27 | SourceLocation PackLoc = PackTok.getLocation(); |
| 28 | |
| 29 | Token Tok; |
| 30 | PP.Lex(Tok); |
| 31 | if (Tok.isNot(tok::l_paren)) { |
| 32 | PP.Diag(Tok.getLocation(), diag::warn_pragma_pack_expected_lparen); |
| 33 | return; |
| 34 | } |
| 35 | |
| 36 | Action::PragmaPackKind Kind = Action::PPK_Default; |
| 37 | IdentifierInfo *Name = 0; |
| 38 | Action::ExprResult Alignment; |
Sebastian Redl | a55e52c | 2008-11-25 22:21:31 +0000 | [diff] [blame^] | 39 | ExprGuard AlignmentGuard(Actions); |
Daniel Dunbar | fcdd8fe | 2008-10-04 19:21:03 +0000 | [diff] [blame] | 40 | SourceLocation LParenLoc = Tok.getLocation(); |
| 41 | PP.Lex(Tok); |
| 42 | if (Tok.is(tok::numeric_constant)) { |
| 43 | Alignment = Actions.ActOnNumericConstant(Tok); |
| 44 | if (Alignment.isInvalid) |
| 45 | return; |
Sebastian Redl | a55e52c | 2008-11-25 22:21:31 +0000 | [diff] [blame^] | 46 | AlignmentGuard.reset(Alignment); |
Daniel Dunbar | fcdd8fe | 2008-10-04 19:21:03 +0000 | [diff] [blame] | 47 | |
| 48 | PP.Lex(Tok); |
| 49 | } else if (Tok.is(tok::identifier)) { |
| 50 | const IdentifierInfo *II = Tok.getIdentifierInfo(); |
Chris Lattner | 08631c5 | 2008-11-23 21:45:46 +0000 | [diff] [blame] | 51 | if (II->isStr("show")) { |
Daniel Dunbar | fcdd8fe | 2008-10-04 19:21:03 +0000 | [diff] [blame] | 52 | Kind = Action::PPK_Show; |
| 53 | PP.Lex(Tok); |
| 54 | } else { |
Chris Lattner | 08631c5 | 2008-11-23 21:45:46 +0000 | [diff] [blame] | 55 | if (II->isStr("push")) { |
Daniel Dunbar | fcdd8fe | 2008-10-04 19:21:03 +0000 | [diff] [blame] | 56 | Kind = Action::PPK_Push; |
Chris Lattner | 08631c5 | 2008-11-23 21:45:46 +0000 | [diff] [blame] | 57 | } else if (II->isStr("pop")) { |
Daniel Dunbar | fcdd8fe | 2008-10-04 19:21:03 +0000 | [diff] [blame] | 58 | Kind = Action::PPK_Pop; |
| 59 | } else { |
| 60 | PP.Diag(Tok.getLocation(), diag::warn_pragma_pack_invalid_action); |
| 61 | return; |
| 62 | } |
| 63 | PP.Lex(Tok); |
| 64 | |
| 65 | if (Tok.is(tok::comma)) { |
| 66 | PP.Lex(Tok); |
| 67 | |
| 68 | if (Tok.is(tok::numeric_constant)) { |
| 69 | Alignment = Actions.ActOnNumericConstant(Tok); |
| 70 | if (Alignment.isInvalid) |
| 71 | return; |
Sebastian Redl | a55e52c | 2008-11-25 22:21:31 +0000 | [diff] [blame^] | 72 | AlignmentGuard.reset(Alignment); |
Daniel Dunbar | fcdd8fe | 2008-10-04 19:21:03 +0000 | [diff] [blame] | 73 | |
| 74 | PP.Lex(Tok); |
| 75 | } else if (Tok.is(tok::identifier)) { |
| 76 | Name = Tok.getIdentifierInfo(); |
| 77 | PP.Lex(Tok); |
| 78 | |
| 79 | if (Tok.is(tok::comma)) { |
| 80 | PP.Lex(Tok); |
| 81 | |
| 82 | if (Tok.isNot(tok::numeric_constant)) { |
Chris Lattner | 08631c5 | 2008-11-23 21:45:46 +0000 | [diff] [blame] | 83 | PP.Diag(Tok.getLocation(), diag::warn_pragma_pack_malformed); |
Daniel Dunbar | fcdd8fe | 2008-10-04 19:21:03 +0000 | [diff] [blame] | 84 | return; |
| 85 | } |
| 86 | |
| 87 | Alignment = Actions.ActOnNumericConstant(Tok); |
| 88 | if (Alignment.isInvalid) |
| 89 | return; |
Sebastian Redl | a55e52c | 2008-11-25 22:21:31 +0000 | [diff] [blame^] | 90 | AlignmentGuard.reset(Alignment); |
Daniel Dunbar | fcdd8fe | 2008-10-04 19:21:03 +0000 | [diff] [blame] | 91 | |
| 92 | PP.Lex(Tok); |
| 93 | } |
| 94 | } else { |
Chris Lattner | 08631c5 | 2008-11-23 21:45:46 +0000 | [diff] [blame] | 95 | PP.Diag(Tok.getLocation(), diag::warn_pragma_pack_malformed); |
Daniel Dunbar | fcdd8fe | 2008-10-04 19:21:03 +0000 | [diff] [blame] | 96 | return; |
| 97 | } |
| 98 | } |
| 99 | } |
| 100 | } |
| 101 | |
| 102 | if (Tok.isNot(tok::r_paren)) { |
| 103 | PP.Diag(Tok.getLocation(), diag::warn_pragma_pack_expected_rparen); |
| 104 | return; |
| 105 | } |
| 106 | |
| 107 | SourceLocation RParenLoc = Tok.getLocation(); |
Sebastian Redl | a55e52c | 2008-11-25 22:21:31 +0000 | [diff] [blame^] | 108 | Actions.ActOnPragmaPack(Kind, Name, AlignmentGuard.take(), PackLoc, |
Daniel Dunbar | fcdd8fe | 2008-10-04 19:21:03 +0000 | [diff] [blame] | 109 | LParenLoc, RParenLoc); |
| 110 | } |
| 111 | |