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" |
Ted Kremenek | 4726d03 | 2009-03-23 22:28:25 +0000 | [diff] [blame] | 18 | #include "clang/Parse/Parser.h" |
Daniel Dunbar | fcdd8fe | 2008-10-04 19:21:03 +0000 | [diff] [blame] | 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) { |
Daniel Dunbar | fcdd8fe | 2008-10-04 19:21:03 +0000 | [diff] [blame] | 26 | SourceLocation PackLoc = PackTok.getLocation(); |
| 27 | |
| 28 | Token Tok; |
| 29 | PP.Lex(Tok); |
| 30 | if (Tok.isNot(tok::l_paren)) { |
Ted Kremenek | 4726d03 | 2009-03-23 22:28:25 +0000 | [diff] [blame] | 31 | PP.Diag(Tok.getLocation(), diag::warn_pragma_expected_lparen) << "pack"; |
Daniel Dunbar | fcdd8fe | 2008-10-04 19:21:03 +0000 | [diff] [blame] | 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(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 39 | PP.Lex(Tok); |
Daniel Dunbar | fcdd8fe | 2008-10-04 19:21:03 +0000 | [diff] [blame] | 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; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 59 | } |
Daniel Dunbar | fcdd8fe | 2008-10-04 19:21:03 +0000 | [diff] [blame] | 60 | PP.Lex(Tok); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 61 | |
Daniel Dunbar | fcdd8fe | 2008-10-04 19:21:03 +0000 | [diff] [blame] | 62 | if (Tok.is(tok::comma)) { |
| 63 | PP.Lex(Tok); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 64 | |
Daniel Dunbar | fcdd8fe | 2008-10-04 19:21:03 +0000 | [diff] [blame] | 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); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 74 | |
Daniel Dunbar | fcdd8fe | 2008-10-04 19:21:03 +0000 | [diff] [blame] | 75 | if (Tok.is(tok::comma)) { |
| 76 | PP.Lex(Tok); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 77 | |
Daniel Dunbar | fcdd8fe | 2008-10-04 19:21:03 +0000 | [diff] [blame] | 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 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 82 | |
Daniel Dunbar | fcdd8fe | 2008-10-04 19:21:03 +0000 | [diff] [blame] | 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)) { |
Ted Kremenek | 4726d03 | 2009-03-23 22:28:25 +0000 | [diff] [blame] | 98 | PP.Diag(Tok.getLocation(), diag::warn_pragma_expected_rparen) << "pack"; |
Daniel Dunbar | fcdd8fe | 2008-10-04 19:21:03 +0000 | [diff] [blame] | 99 | return; |
| 100 | } |
| 101 | |
Daniel Dunbar | 861800c | 2010-05-26 23:29:06 +0000 | [diff] [blame^] | 102 | SourceLocation RParenLoc = Tok.getLocation(); |
Eli Friedman | 9991479 | 2009-06-05 00:49:58 +0000 | [diff] [blame] | 103 | PP.Lex(Tok); |
| 104 | if (Tok.isNot(tok::eom)) { |
| 105 | PP.Diag(Tok.getLocation(), diag::warn_pragma_extra_tokens_at_eol) << "pack"; |
| 106 | return; |
| 107 | } |
| 108 | |
Sebastian Redl | effa8d1 | 2008-12-10 00:02:53 +0000 | [diff] [blame] | 109 | Actions.ActOnPragmaPack(Kind, Name, Alignment.release(), PackLoc, |
Daniel Dunbar | fcdd8fe | 2008-10-04 19:21:03 +0000 | [diff] [blame] | 110 | LParenLoc, RParenLoc); |
| 111 | } |
| 112 | |
Daniel Dunbar | 861800c | 2010-05-26 23:29:06 +0000 | [diff] [blame^] | 113 | // #pragma 'options' 'align' '=' {'natural', 'mac68k', 'power', 'reset'} |
| 114 | void PragmaOptionsHandler::HandlePragma(Preprocessor &PP, Token &OptionsTok) { |
| 115 | SourceLocation OptionsLoc = OptionsTok.getLocation(); |
| 116 | |
| 117 | Token Tok; |
| 118 | PP.Lex(Tok); |
| 119 | if (Tok.isNot(tok::identifier) || !Tok.getIdentifierInfo()->isStr("align")) { |
| 120 | PP.Diag(Tok.getLocation(), diag::warn_pragma_options_expected_align); |
| 121 | return; |
| 122 | } |
| 123 | |
| 124 | PP.Lex(Tok); |
| 125 | if (Tok.isNot(tok::equal)) { |
| 126 | PP.Diag(Tok.getLocation(), diag::warn_pragma_options_expected_equal); |
| 127 | return; |
| 128 | } |
| 129 | |
| 130 | PP.Lex(Tok); |
| 131 | if (Tok.isNot(tok::identifier)) { |
| 132 | PP.Diag(Tok.getLocation(), diag::warn_pragma_expected_identifier) |
| 133 | << "options"; |
| 134 | return; |
| 135 | } |
| 136 | |
| 137 | Action::PragmaOptionsAlignKind Kind = Action::POAK_Natural; |
| 138 | const IdentifierInfo *II = Tok.getIdentifierInfo(); |
| 139 | if (II->isStr("natural")) |
| 140 | Kind = Action::POAK_Natural; |
| 141 | else if (II->isStr("power")) |
| 142 | Kind = Action::POAK_Power; |
| 143 | else if (II->isStr("mac68k")) |
| 144 | Kind = Action::POAK_Mac68k; |
| 145 | else if (II->isStr("reset")) |
| 146 | Kind = Action::POAK_Reset; |
| 147 | else { |
| 148 | PP.Diag(Tok.getLocation(), diag::warn_pragma_options_invalid_option); |
| 149 | return; |
| 150 | } |
| 151 | |
| 152 | SourceLocation KindLoc = Tok.getLocation(); |
| 153 | PP.Lex(Tok); |
| 154 | if (Tok.isNot(tok::eom)) { |
| 155 | PP.Diag(Tok.getLocation(), diag::warn_pragma_extra_tokens_at_eol) |
| 156 | << "options"; |
| 157 | return; |
| 158 | } |
| 159 | |
| 160 | Actions.ActOnPragmaOptionsAlign(Kind, OptionsLoc, KindLoc); |
| 161 | } |
| 162 | |
Ted Kremenek | 4726d03 | 2009-03-23 22:28:25 +0000 | [diff] [blame] | 163 | // #pragma unused(identifier) |
| 164 | void PragmaUnusedHandler::HandlePragma(Preprocessor &PP, Token &UnusedTok) { |
| 165 | // FIXME: Should we be expanding macros here? My guess is no. |
| 166 | SourceLocation UnusedLoc = UnusedTok.getLocation(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 167 | |
Ted Kremenek | 4726d03 | 2009-03-23 22:28:25 +0000 | [diff] [blame] | 168 | // Lex the left '('. |
| 169 | Token Tok; |
| 170 | PP.Lex(Tok); |
| 171 | if (Tok.isNot(tok::l_paren)) { |
| 172 | PP.Diag(Tok.getLocation(), diag::warn_pragma_expected_lparen) << "unused"; |
| 173 | return; |
| 174 | } |
| 175 | SourceLocation LParenLoc = Tok.getLocation(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 176 | |
Ted Kremenek | 4726d03 | 2009-03-23 22:28:25 +0000 | [diff] [blame] | 177 | // Lex the declaration reference(s). |
Ted Kremenek | 7a02a37 | 2009-08-03 23:24:57 +0000 | [diff] [blame] | 178 | llvm::SmallVector<Token, 5> Identifiers; |
Ted Kremenek | 4726d03 | 2009-03-23 22:28:25 +0000 | [diff] [blame] | 179 | SourceLocation RParenLoc; |
| 180 | bool LexID = true; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 181 | |
Ted Kremenek | 4726d03 | 2009-03-23 22:28:25 +0000 | [diff] [blame] | 182 | while (true) { |
| 183 | PP.Lex(Tok); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 184 | |
Ted Kremenek | 4726d03 | 2009-03-23 22:28:25 +0000 | [diff] [blame] | 185 | if (LexID) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 186 | if (Tok.is(tok::identifier)) { |
Ted Kremenek | 7a02a37 | 2009-08-03 23:24:57 +0000 | [diff] [blame] | 187 | Identifiers.push_back(Tok); |
Ted Kremenek | 4726d03 | 2009-03-23 22:28:25 +0000 | [diff] [blame] | 188 | LexID = false; |
| 189 | continue; |
| 190 | } |
| 191 | |
Ted Kremenek | 7a02a37 | 2009-08-03 23:24:57 +0000 | [diff] [blame] | 192 | // Illegal token! |
Ted Kremenek | 4726d03 | 2009-03-23 22:28:25 +0000 | [diff] [blame] | 193 | PP.Diag(Tok.getLocation(), diag::warn_pragma_unused_expected_var); |
| 194 | return; |
| 195 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 196 | |
Ted Kremenek | 4726d03 | 2009-03-23 22:28:25 +0000 | [diff] [blame] | 197 | // We are execting a ')' or a ','. |
| 198 | if (Tok.is(tok::comma)) { |
| 199 | LexID = true; |
| 200 | continue; |
| 201 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 202 | |
Ted Kremenek | 4726d03 | 2009-03-23 22:28:25 +0000 | [diff] [blame] | 203 | if (Tok.is(tok::r_paren)) { |
| 204 | RParenLoc = Tok.getLocation(); |
| 205 | break; |
| 206 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 207 | |
Ted Kremenek | 7a02a37 | 2009-08-03 23:24:57 +0000 | [diff] [blame] | 208 | // Illegal token! |
Ted Kremenek | 4726d03 | 2009-03-23 22:28:25 +0000 | [diff] [blame] | 209 | PP.Diag(Tok.getLocation(), diag::warn_pragma_unused_expected_punc); |
| 210 | return; |
| 211 | } |
Eli Friedman | 9991479 | 2009-06-05 00:49:58 +0000 | [diff] [blame] | 212 | |
| 213 | PP.Lex(Tok); |
| 214 | if (Tok.isNot(tok::eom)) { |
| 215 | PP.Diag(Tok.getLocation(), diag::warn_pragma_extra_tokens_at_eol) << |
| 216 | "unused"; |
| 217 | return; |
| 218 | } |
| 219 | |
Ted Kremenek | 4726d03 | 2009-03-23 22:28:25 +0000 | [diff] [blame] | 220 | // Verify that we have a location for the right parenthesis. |
| 221 | assert(RParenLoc.isValid() && "Valid '#pragma unused' must have ')'"); |
Ted Kremenek | 7a02a37 | 2009-08-03 23:24:57 +0000 | [diff] [blame] | 222 | assert(!Identifiers.empty() && "Valid '#pragma unused' must have arguments"); |
Ted Kremenek | 4726d03 | 2009-03-23 22:28:25 +0000 | [diff] [blame] | 223 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 224 | // Perform the action to handle the pragma. |
Ted Kremenek | 7a02a37 | 2009-08-03 23:24:57 +0000 | [diff] [blame] | 225 | Actions.ActOnPragmaUnused(Identifiers.data(), Identifiers.size(), |
| 226 | parser.CurScope, UnusedLoc, LParenLoc, RParenLoc); |
Ted Kremenek | 4726d03 | 2009-03-23 22:28:25 +0000 | [diff] [blame] | 227 | } |
Eli Friedman | 9991479 | 2009-06-05 00:49:58 +0000 | [diff] [blame] | 228 | |
| 229 | // #pragma weak identifier |
| 230 | // #pragma weak identifier '=' identifier |
| 231 | void PragmaWeakHandler::HandlePragma(Preprocessor &PP, Token &WeakTok) { |
| 232 | // FIXME: Should we be expanding macros here? My guess is no. |
| 233 | SourceLocation WeakLoc = WeakTok.getLocation(); |
| 234 | |
| 235 | Token Tok; |
| 236 | PP.Lex(Tok); |
| 237 | if (Tok.isNot(tok::identifier)) { |
| 238 | PP.Diag(Tok.getLocation(), diag::warn_pragma_expected_identifier) << "weak"; |
| 239 | return; |
| 240 | } |
| 241 | |
| 242 | IdentifierInfo *WeakName = Tok.getIdentifierInfo(), *AliasName = 0; |
| 243 | SourceLocation WeakNameLoc = Tok.getLocation(), AliasNameLoc; |
| 244 | |
| 245 | PP.Lex(Tok); |
| 246 | if (Tok.is(tok::equal)) { |
| 247 | PP.Lex(Tok); |
| 248 | if (Tok.isNot(tok::identifier)) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 249 | PP.Diag(Tok.getLocation(), diag::warn_pragma_expected_identifier) |
Eli Friedman | 9991479 | 2009-06-05 00:49:58 +0000 | [diff] [blame] | 250 | << "weak"; |
| 251 | return; |
| 252 | } |
| 253 | AliasName = Tok.getIdentifierInfo(); |
| 254 | AliasNameLoc = Tok.getLocation(); |
| 255 | PP.Lex(Tok); |
| 256 | } |
| 257 | |
| 258 | if (Tok.isNot(tok::eom)) { |
| 259 | PP.Diag(Tok.getLocation(), diag::warn_pragma_extra_tokens_at_eol) << "weak"; |
| 260 | return; |
| 261 | } |
| 262 | |
| 263 | if (AliasName) { |
| 264 | Actions.ActOnPragmaWeakAlias(WeakName, AliasName, WeakLoc, WeakNameLoc, |
| 265 | AliasNameLoc); |
| 266 | } else { |
| 267 | Actions.ActOnPragmaWeakID(WeakName, WeakLoc, WeakNameLoc); |
| 268 | } |
| 269 | } |