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" |
Chris Lattner | 500d329 | 2009-01-29 05:15:15 +0000 | [diff] [blame^] | 15 | #include "clang/Parse/ParseDiagnostic.h" |
Daniel Dunbar | fcdd8fe | 2008-10-04 19:21:03 +0000 | [diff] [blame] | 16 | #include "clang/Lex/Preprocessor.h" |
| 17 | #include "clang/Parse/Action.h" |
| 18 | using namespace clang; |
| 19 | |
| 20 | // #pragma pack(...) comes in the following delicious flavors: |
| 21 | // pack '(' [integer] ')' |
| 22 | // pack '(' 'show' ')' |
| 23 | // pack '(' ('push' | 'pop') [',' identifier] [, integer] ')' |
| 24 | void PragmaPackHandler::HandlePragma(Preprocessor &PP, Token &PackTok) { |
| 25 | // FIXME: Should we be expanding macros here? My guess is no. |
| 26 | SourceLocation PackLoc = PackTok.getLocation(); |
| 27 | |
| 28 | Token Tok; |
| 29 | PP.Lex(Tok); |
| 30 | if (Tok.isNot(tok::l_paren)) { |
| 31 | PP.Diag(Tok.getLocation(), diag::warn_pragma_pack_expected_lparen); |
| 32 | return; |
| 33 | } |
| 34 | |
| 35 | Action::PragmaPackKind Kind = Action::PPK_Default; |
| 36 | IdentifierInfo *Name = 0; |
Sebastian Redl | 15faa7f | 2008-12-09 20:22:58 +0000 | [diff] [blame] | 37 | Action::OwningExprResult Alignment(Actions); |
Daniel Dunbar | fcdd8fe | 2008-10-04 19:21:03 +0000 | [diff] [blame] | 38 | SourceLocation LParenLoc = Tok.getLocation(); |
| 39 | PP.Lex(Tok); |
| 40 | if (Tok.is(tok::numeric_constant)) { |
| 41 | Alignment = Actions.ActOnNumericConstant(Tok); |
Sebastian Redl | 0e9eabc | 2008-12-09 13:15:23 +0000 | [diff] [blame] | 42 | if (Alignment.isInvalid()) |
Daniel Dunbar | fcdd8fe | 2008-10-04 19:21:03 +0000 | [diff] [blame] | 43 | return; |
| 44 | |
| 45 | PP.Lex(Tok); |
| 46 | } else if (Tok.is(tok::identifier)) { |
| 47 | const IdentifierInfo *II = Tok.getIdentifierInfo(); |
Chris Lattner | 08631c5 | 2008-11-23 21:45:46 +0000 | [diff] [blame] | 48 | if (II->isStr("show")) { |
Daniel Dunbar | fcdd8fe | 2008-10-04 19:21:03 +0000 | [diff] [blame] | 49 | Kind = Action::PPK_Show; |
| 50 | PP.Lex(Tok); |
| 51 | } else { |
Chris Lattner | 08631c5 | 2008-11-23 21:45:46 +0000 | [diff] [blame] | 52 | if (II->isStr("push")) { |
Daniel Dunbar | fcdd8fe | 2008-10-04 19:21:03 +0000 | [diff] [blame] | 53 | Kind = Action::PPK_Push; |
Chris Lattner | 08631c5 | 2008-11-23 21:45:46 +0000 | [diff] [blame] | 54 | } else if (II->isStr("pop")) { |
Daniel Dunbar | fcdd8fe | 2008-10-04 19:21:03 +0000 | [diff] [blame] | 55 | Kind = Action::PPK_Pop; |
| 56 | } else { |
| 57 | PP.Diag(Tok.getLocation(), diag::warn_pragma_pack_invalid_action); |
| 58 | return; |
| 59 | } |
| 60 | PP.Lex(Tok); |
| 61 | |
| 62 | if (Tok.is(tok::comma)) { |
| 63 | PP.Lex(Tok); |
| 64 | |
| 65 | if (Tok.is(tok::numeric_constant)) { |
| 66 | Alignment = Actions.ActOnNumericConstant(Tok); |
Sebastian Redl | 0e9eabc | 2008-12-09 13:15:23 +0000 | [diff] [blame] | 67 | if (Alignment.isInvalid()) |
Daniel Dunbar | fcdd8fe | 2008-10-04 19:21:03 +0000 | [diff] [blame] | 68 | return; |
| 69 | |
| 70 | PP.Lex(Tok); |
| 71 | } else if (Tok.is(tok::identifier)) { |
| 72 | Name = Tok.getIdentifierInfo(); |
| 73 | PP.Lex(Tok); |
| 74 | |
| 75 | if (Tok.is(tok::comma)) { |
| 76 | PP.Lex(Tok); |
| 77 | |
| 78 | if (Tok.isNot(tok::numeric_constant)) { |
Chris Lattner | 08631c5 | 2008-11-23 21:45:46 +0000 | [diff] [blame] | 79 | PP.Diag(Tok.getLocation(), diag::warn_pragma_pack_malformed); |
Daniel Dunbar | fcdd8fe | 2008-10-04 19:21:03 +0000 | [diff] [blame] | 80 | return; |
| 81 | } |
| 82 | |
| 83 | Alignment = Actions.ActOnNumericConstant(Tok); |
Sebastian Redl | 0e9eabc | 2008-12-09 13:15:23 +0000 | [diff] [blame] | 84 | if (Alignment.isInvalid()) |
Daniel Dunbar | fcdd8fe | 2008-10-04 19:21:03 +0000 | [diff] [blame] | 85 | return; |
| 86 | |
| 87 | PP.Lex(Tok); |
| 88 | } |
| 89 | } else { |
Chris Lattner | 08631c5 | 2008-11-23 21:45:46 +0000 | [diff] [blame] | 90 | PP.Diag(Tok.getLocation(), diag::warn_pragma_pack_malformed); |
Daniel Dunbar | fcdd8fe | 2008-10-04 19:21:03 +0000 | [diff] [blame] | 91 | return; |
| 92 | } |
| 93 | } |
| 94 | } |
Sebastian Redl | 0e9eabc | 2008-12-09 13:15:23 +0000 | [diff] [blame] | 95 | } |
Daniel Dunbar | fcdd8fe | 2008-10-04 19:21:03 +0000 | [diff] [blame] | 96 | |
| 97 | if (Tok.isNot(tok::r_paren)) { |
| 98 | PP.Diag(Tok.getLocation(), diag::warn_pragma_pack_expected_rparen); |
| 99 | return; |
| 100 | } |
| 101 | |
| 102 | SourceLocation RParenLoc = Tok.getLocation(); |
Sebastian Redl | effa8d1 | 2008-12-10 00:02:53 +0000 | [diff] [blame] | 103 | Actions.ActOnPragmaPack(Kind, Name, Alignment.release(), PackLoc, |
Daniel Dunbar | fcdd8fe | 2008-10-04 19:21:03 +0000 | [diff] [blame] | 104 | LParenLoc, RParenLoc); |
| 105 | } |
| 106 | |