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 | cbb98ed | 2010-07-31 19:17:07 +0000 | [diff] [blame^] | 113 | // #pragma 'align' '=' {'native','natural','mac68k','power','reset'} |
| 114 | // #pragma 'options 'align' '=' {'native','natural','mac68k','power','reset'} |
| 115 | static void ParseAlignPragma(Action &Actions, Preprocessor &PP, Token &FirstTok, |
| 116 | bool IsOptions) { |
Daniel Dunbar | 861800c | 2010-05-26 23:29:06 +0000 | [diff] [blame] | 117 | Token Tok; |
Daniel Dunbar | cbb98ed | 2010-07-31 19:17:07 +0000 | [diff] [blame^] | 118 | |
| 119 | if (IsOptions) { |
| 120 | PP.Lex(Tok); |
| 121 | if (Tok.isNot(tok::identifier) || |
| 122 | !Tok.getIdentifierInfo()->isStr("align")) { |
| 123 | PP.Diag(Tok.getLocation(), diag::warn_pragma_options_expected_align); |
| 124 | return; |
| 125 | } |
Daniel Dunbar | 861800c | 2010-05-26 23:29:06 +0000 | [diff] [blame] | 126 | } |
Daniel Dunbar | 638e7cf | 2010-05-27 18:42:09 +0000 | [diff] [blame] | 127 | |
Daniel Dunbar | 861800c | 2010-05-26 23:29:06 +0000 | [diff] [blame] | 128 | PP.Lex(Tok); |
| 129 | if (Tok.isNot(tok::equal)) { |
Daniel Dunbar | cbb98ed | 2010-07-31 19:17:07 +0000 | [diff] [blame^] | 130 | PP.Diag(Tok.getLocation(), diag::warn_pragma_align_expected_equal) |
| 131 | << IsOptions; |
Daniel Dunbar | 861800c | 2010-05-26 23:29:06 +0000 | [diff] [blame] | 132 | return; |
| 133 | } |
| 134 | |
| 135 | PP.Lex(Tok); |
| 136 | if (Tok.isNot(tok::identifier)) { |
| 137 | PP.Diag(Tok.getLocation(), diag::warn_pragma_expected_identifier) |
Daniel Dunbar | cbb98ed | 2010-07-31 19:17:07 +0000 | [diff] [blame^] | 138 | << (IsOptions ? "options" : "align"); |
Daniel Dunbar | 861800c | 2010-05-26 23:29:06 +0000 | [diff] [blame] | 139 | return; |
| 140 | } |
| 141 | |
| 142 | Action::PragmaOptionsAlignKind Kind = Action::POAK_Natural; |
| 143 | const IdentifierInfo *II = Tok.getIdentifierInfo(); |
Daniel Dunbar | 638e7cf | 2010-05-27 18:42:09 +0000 | [diff] [blame] | 144 | if (II->isStr("native")) |
| 145 | Kind = Action::POAK_Native; |
| 146 | else if (II->isStr("natural")) |
Daniel Dunbar | 861800c | 2010-05-26 23:29:06 +0000 | [diff] [blame] | 147 | Kind = Action::POAK_Natural; |
Daniel Dunbar | 6f73914 | 2010-05-27 18:42:17 +0000 | [diff] [blame] | 148 | else if (II->isStr("packed")) |
| 149 | Kind = Action::POAK_Packed; |
Daniel Dunbar | 861800c | 2010-05-26 23:29:06 +0000 | [diff] [blame] | 150 | else if (II->isStr("power")) |
| 151 | Kind = Action::POAK_Power; |
| 152 | else if (II->isStr("mac68k")) |
| 153 | Kind = Action::POAK_Mac68k; |
| 154 | else if (II->isStr("reset")) |
| 155 | Kind = Action::POAK_Reset; |
| 156 | else { |
Daniel Dunbar | cbb98ed | 2010-07-31 19:17:07 +0000 | [diff] [blame^] | 157 | PP.Diag(Tok.getLocation(), diag::warn_pragma_align_invalid_option) |
| 158 | << IsOptions; |
Daniel Dunbar | 861800c | 2010-05-26 23:29:06 +0000 | [diff] [blame] | 159 | return; |
| 160 | } |
| 161 | |
| 162 | SourceLocation KindLoc = Tok.getLocation(); |
| 163 | PP.Lex(Tok); |
| 164 | if (Tok.isNot(tok::eom)) { |
| 165 | PP.Diag(Tok.getLocation(), diag::warn_pragma_extra_tokens_at_eol) |
Daniel Dunbar | cbb98ed | 2010-07-31 19:17:07 +0000 | [diff] [blame^] | 166 | << (IsOptions ? "options" : "align"); |
Daniel Dunbar | 861800c | 2010-05-26 23:29:06 +0000 | [diff] [blame] | 167 | return; |
| 168 | } |
| 169 | |
Daniel Dunbar | cbb98ed | 2010-07-31 19:17:07 +0000 | [diff] [blame^] | 170 | Actions.ActOnPragmaOptionsAlign(Kind, FirstTok.getLocation(), KindLoc); |
| 171 | } |
| 172 | |
| 173 | void PragmaAlignHandler::HandlePragma(Preprocessor &PP, Token &AlignTok) { |
| 174 | ParseAlignPragma(Actions, PP, AlignTok, /*IsOptions=*/false); |
| 175 | } |
| 176 | |
| 177 | void PragmaOptionsHandler::HandlePragma(Preprocessor &PP, Token &OptionsTok) { |
| 178 | ParseAlignPragma(Actions, PP, OptionsTok, /*IsOptions=*/true); |
Daniel Dunbar | 861800c | 2010-05-26 23:29:06 +0000 | [diff] [blame] | 179 | } |
| 180 | |
Ted Kremenek | 4726d03 | 2009-03-23 22:28:25 +0000 | [diff] [blame] | 181 | // #pragma unused(identifier) |
| 182 | void PragmaUnusedHandler::HandlePragma(Preprocessor &PP, Token &UnusedTok) { |
| 183 | // FIXME: Should we be expanding macros here? My guess is no. |
| 184 | SourceLocation UnusedLoc = UnusedTok.getLocation(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 185 | |
Ted Kremenek | 4726d03 | 2009-03-23 22:28:25 +0000 | [diff] [blame] | 186 | // Lex the left '('. |
| 187 | Token Tok; |
| 188 | PP.Lex(Tok); |
| 189 | if (Tok.isNot(tok::l_paren)) { |
| 190 | PP.Diag(Tok.getLocation(), diag::warn_pragma_expected_lparen) << "unused"; |
| 191 | return; |
| 192 | } |
| 193 | SourceLocation LParenLoc = Tok.getLocation(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 194 | |
Ted Kremenek | 4726d03 | 2009-03-23 22:28:25 +0000 | [diff] [blame] | 195 | // Lex the declaration reference(s). |
Ted Kremenek | 7a02a37 | 2009-08-03 23:24:57 +0000 | [diff] [blame] | 196 | llvm::SmallVector<Token, 5> Identifiers; |
Ted Kremenek | 4726d03 | 2009-03-23 22:28:25 +0000 | [diff] [blame] | 197 | SourceLocation RParenLoc; |
| 198 | bool LexID = true; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 199 | |
Ted Kremenek | 4726d03 | 2009-03-23 22:28:25 +0000 | [diff] [blame] | 200 | while (true) { |
| 201 | PP.Lex(Tok); |
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 (LexID) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 204 | if (Tok.is(tok::identifier)) { |
Ted Kremenek | 7a02a37 | 2009-08-03 23:24:57 +0000 | [diff] [blame] | 205 | Identifiers.push_back(Tok); |
Ted Kremenek | 4726d03 | 2009-03-23 22:28:25 +0000 | [diff] [blame] | 206 | LexID = false; |
| 207 | continue; |
| 208 | } |
| 209 | |
Ted Kremenek | 7a02a37 | 2009-08-03 23:24:57 +0000 | [diff] [blame] | 210 | // Illegal token! |
Ted Kremenek | 4726d03 | 2009-03-23 22:28:25 +0000 | [diff] [blame] | 211 | PP.Diag(Tok.getLocation(), diag::warn_pragma_unused_expected_var); |
| 212 | return; |
| 213 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 214 | |
Ted Kremenek | 4726d03 | 2009-03-23 22:28:25 +0000 | [diff] [blame] | 215 | // We are execting a ')' or a ','. |
| 216 | if (Tok.is(tok::comma)) { |
| 217 | LexID = true; |
| 218 | continue; |
| 219 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 220 | |
Ted Kremenek | 4726d03 | 2009-03-23 22:28:25 +0000 | [diff] [blame] | 221 | if (Tok.is(tok::r_paren)) { |
| 222 | RParenLoc = Tok.getLocation(); |
| 223 | break; |
| 224 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 225 | |
Ted Kremenek | 7a02a37 | 2009-08-03 23:24:57 +0000 | [diff] [blame] | 226 | // Illegal token! |
Ted Kremenek | 4726d03 | 2009-03-23 22:28:25 +0000 | [diff] [blame] | 227 | PP.Diag(Tok.getLocation(), diag::warn_pragma_unused_expected_punc); |
| 228 | return; |
| 229 | } |
Eli Friedman | 9991479 | 2009-06-05 00:49:58 +0000 | [diff] [blame] | 230 | |
| 231 | PP.Lex(Tok); |
| 232 | if (Tok.isNot(tok::eom)) { |
| 233 | PP.Diag(Tok.getLocation(), diag::warn_pragma_extra_tokens_at_eol) << |
| 234 | "unused"; |
| 235 | return; |
| 236 | } |
| 237 | |
Ted Kremenek | 4726d03 | 2009-03-23 22:28:25 +0000 | [diff] [blame] | 238 | // Verify that we have a location for the right parenthesis. |
| 239 | assert(RParenLoc.isValid() && "Valid '#pragma unused' must have ')'"); |
Ted Kremenek | 7a02a37 | 2009-08-03 23:24:57 +0000 | [diff] [blame] | 240 | assert(!Identifiers.empty() && "Valid '#pragma unused' must have arguments"); |
Ted Kremenek | 4726d03 | 2009-03-23 22:28:25 +0000 | [diff] [blame] | 241 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 242 | // Perform the action to handle the pragma. |
Ted Kremenek | 7a02a37 | 2009-08-03 23:24:57 +0000 | [diff] [blame] | 243 | Actions.ActOnPragmaUnused(Identifiers.data(), Identifiers.size(), |
Douglas Gregor | 23c94db | 2010-07-02 17:43:08 +0000 | [diff] [blame] | 244 | parser.getCurScope(), UnusedLoc, LParenLoc, RParenLoc); |
Ted Kremenek | 4726d03 | 2009-03-23 22:28:25 +0000 | [diff] [blame] | 245 | } |
Eli Friedman | 9991479 | 2009-06-05 00:49:58 +0000 | [diff] [blame] | 246 | |
| 247 | // #pragma weak identifier |
| 248 | // #pragma weak identifier '=' identifier |
| 249 | void PragmaWeakHandler::HandlePragma(Preprocessor &PP, Token &WeakTok) { |
| 250 | // FIXME: Should we be expanding macros here? My guess is no. |
| 251 | SourceLocation WeakLoc = WeakTok.getLocation(); |
| 252 | |
| 253 | Token Tok; |
| 254 | PP.Lex(Tok); |
| 255 | if (Tok.isNot(tok::identifier)) { |
| 256 | PP.Diag(Tok.getLocation(), diag::warn_pragma_expected_identifier) << "weak"; |
| 257 | return; |
| 258 | } |
| 259 | |
| 260 | IdentifierInfo *WeakName = Tok.getIdentifierInfo(), *AliasName = 0; |
| 261 | SourceLocation WeakNameLoc = Tok.getLocation(), AliasNameLoc; |
| 262 | |
| 263 | PP.Lex(Tok); |
| 264 | if (Tok.is(tok::equal)) { |
| 265 | PP.Lex(Tok); |
| 266 | if (Tok.isNot(tok::identifier)) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 267 | PP.Diag(Tok.getLocation(), diag::warn_pragma_expected_identifier) |
Eli Friedman | 9991479 | 2009-06-05 00:49:58 +0000 | [diff] [blame] | 268 | << "weak"; |
| 269 | return; |
| 270 | } |
| 271 | AliasName = Tok.getIdentifierInfo(); |
| 272 | AliasNameLoc = Tok.getLocation(); |
| 273 | PP.Lex(Tok); |
| 274 | } |
| 275 | |
| 276 | if (Tok.isNot(tok::eom)) { |
| 277 | PP.Diag(Tok.getLocation(), diag::warn_pragma_extra_tokens_at_eol) << "weak"; |
| 278 | return; |
| 279 | } |
| 280 | |
| 281 | if (AliasName) { |
| 282 | Actions.ActOnPragmaWeakAlias(WeakName, AliasName, WeakLoc, WeakNameLoc, |
| 283 | AliasNameLoc); |
| 284 | } else { |
| 285 | Actions.ActOnPragmaWeakID(WeakName, WeakLoc, WeakNameLoc); |
| 286 | } |
| 287 | } |