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" |
Ted Kremenek | 4726d03 | 2009-03-23 22:28:25 +0000 | [diff] [blame] | 16 | #include "clang/Parse/Parser.h" |
John McCall | 1951085 | 2010-08-20 18:27:03 +0000 | [diff] [blame] | 17 | #include "clang/Lex/Preprocessor.h" |
Daniel Dunbar | fcdd8fe | 2008-10-04 19:21:03 +0000 | [diff] [blame] | 18 | using namespace clang; |
| 19 | |
Argyrios Kyrtzidis | b918d0f | 2011-01-17 18:58:44 +0000 | [diff] [blame] | 20 | /// \brief Handle the annotation token produced for #pragma unused(...) |
| 21 | /// |
| 22 | /// Each annot_pragma_unused is followed by the argument token so e.g. |
| 23 | /// "#pragma unused(x,y)" becomes: |
| 24 | /// annot_pragma_unused 'x' annot_pragma_unused 'y' |
| 25 | void Parser::HandlePragmaUnused() { |
| 26 | assert(Tok.is(tok::annot_pragma_unused)); |
| 27 | SourceLocation UnusedLoc = ConsumeToken(); |
| 28 | Actions.ActOnPragmaUnused(Tok, getCurScope(), UnusedLoc); |
| 29 | ConsumeToken(); // The argument token. |
| 30 | } |
Eli Friedman | aa8b0d1 | 2010-08-05 06:57:20 +0000 | [diff] [blame] | 31 | |
| 32 | // #pragma GCC visibility comes in two variants: |
| 33 | // 'push' '(' [visibility] ')' |
| 34 | // 'pop' |
Douglas Gregor | 80c60f7 | 2010-09-09 22:45:38 +0000 | [diff] [blame] | 35 | void PragmaGCCVisibilityHandler::HandlePragma(Preprocessor &PP, |
| 36 | PragmaIntroducerKind Introducer, |
| 37 | Token &VisTok) { |
Eli Friedman | aa8b0d1 | 2010-08-05 06:57:20 +0000 | [diff] [blame] | 38 | SourceLocation VisLoc = VisTok.getLocation(); |
| 39 | |
| 40 | Token Tok; |
Joerg Sonnenberger | e23af2a | 2011-07-20 01:03:50 +0000 | [diff] [blame^] | 41 | PP.LexUnexpandedToken(Tok); |
Eli Friedman | aa8b0d1 | 2010-08-05 06:57:20 +0000 | [diff] [blame] | 42 | |
| 43 | const IdentifierInfo *PushPop = Tok.getIdentifierInfo(); |
| 44 | |
| 45 | bool IsPush; |
| 46 | const IdentifierInfo *VisType; |
| 47 | if (PushPop && PushPop->isStr("pop")) { |
| 48 | IsPush = false; |
| 49 | VisType = 0; |
| 50 | } else if (PushPop && PushPop->isStr("push")) { |
| 51 | IsPush = true; |
Joerg Sonnenberger | e23af2a | 2011-07-20 01:03:50 +0000 | [diff] [blame^] | 52 | PP.LexUnexpandedToken(Tok); |
Eli Friedman | aa8b0d1 | 2010-08-05 06:57:20 +0000 | [diff] [blame] | 53 | if (Tok.isNot(tok::l_paren)) { |
| 54 | PP.Diag(Tok.getLocation(), diag::warn_pragma_expected_lparen) |
| 55 | << "visibility"; |
| 56 | return; |
| 57 | } |
Joerg Sonnenberger | e23af2a | 2011-07-20 01:03:50 +0000 | [diff] [blame^] | 58 | PP.LexUnexpandedToken(Tok); |
Eli Friedman | aa8b0d1 | 2010-08-05 06:57:20 +0000 | [diff] [blame] | 59 | VisType = Tok.getIdentifierInfo(); |
| 60 | if (!VisType) { |
| 61 | PP.Diag(Tok.getLocation(), diag::warn_pragma_expected_identifier) |
| 62 | << "visibility"; |
| 63 | return; |
| 64 | } |
Joerg Sonnenberger | e23af2a | 2011-07-20 01:03:50 +0000 | [diff] [blame^] | 65 | PP.LexUnexpandedToken(Tok); |
Eli Friedman | aa8b0d1 | 2010-08-05 06:57:20 +0000 | [diff] [blame] | 66 | if (Tok.isNot(tok::r_paren)) { |
| 67 | PP.Diag(Tok.getLocation(), diag::warn_pragma_expected_rparen) |
| 68 | << "visibility"; |
| 69 | return; |
| 70 | } |
| 71 | } else { |
| 72 | PP.Diag(Tok.getLocation(), diag::warn_pragma_expected_identifier) |
| 73 | << "visibility"; |
| 74 | return; |
| 75 | } |
Joerg Sonnenberger | e23af2a | 2011-07-20 01:03:50 +0000 | [diff] [blame^] | 76 | PP.LexUnexpandedToken(Tok); |
Peter Collingbourne | 8402155 | 2011-02-28 02:37:51 +0000 | [diff] [blame] | 77 | if (Tok.isNot(tok::eod)) { |
Eli Friedman | aa8b0d1 | 2010-08-05 06:57:20 +0000 | [diff] [blame] | 78 | PP.Diag(Tok.getLocation(), diag::warn_pragma_extra_tokens_at_eol) |
| 79 | << "visibility"; |
| 80 | return; |
| 81 | } |
| 82 | |
| 83 | Actions.ActOnPragmaVisibility(IsPush, VisType, VisLoc); |
| 84 | } |
| 85 | |
Daniel Dunbar | fcdd8fe | 2008-10-04 19:21:03 +0000 | [diff] [blame] | 86 | // #pragma pack(...) comes in the following delicious flavors: |
| 87 | // pack '(' [integer] ')' |
| 88 | // pack '(' 'show' ')' |
| 89 | // pack '(' ('push' | 'pop') [',' identifier] [, integer] ')' |
Douglas Gregor | 80c60f7 | 2010-09-09 22:45:38 +0000 | [diff] [blame] | 90 | void PragmaPackHandler::HandlePragma(Preprocessor &PP, |
| 91 | PragmaIntroducerKind Introducer, |
| 92 | Token &PackTok) { |
Daniel Dunbar | fcdd8fe | 2008-10-04 19:21:03 +0000 | [diff] [blame] | 93 | SourceLocation PackLoc = PackTok.getLocation(); |
| 94 | |
| 95 | Token Tok; |
| 96 | PP.Lex(Tok); |
| 97 | if (Tok.isNot(tok::l_paren)) { |
Ted Kremenek | 4726d03 | 2009-03-23 22:28:25 +0000 | [diff] [blame] | 98 | PP.Diag(Tok.getLocation(), diag::warn_pragma_expected_lparen) << "pack"; |
Daniel Dunbar | fcdd8fe | 2008-10-04 19:21:03 +0000 | [diff] [blame] | 99 | return; |
| 100 | } |
| 101 | |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 102 | Sema::PragmaPackKind Kind = Sema::PPK_Default; |
Daniel Dunbar | fcdd8fe | 2008-10-04 19:21:03 +0000 | [diff] [blame] | 103 | IdentifierInfo *Name = 0; |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 104 | ExprResult Alignment; |
Daniel Dunbar | fcdd8fe | 2008-10-04 19:21:03 +0000 | [diff] [blame] | 105 | SourceLocation LParenLoc = Tok.getLocation(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 106 | PP.Lex(Tok); |
Daniel Dunbar | fcdd8fe | 2008-10-04 19:21:03 +0000 | [diff] [blame] | 107 | if (Tok.is(tok::numeric_constant)) { |
| 108 | Alignment = Actions.ActOnNumericConstant(Tok); |
Sebastian Redl | 0e9eabc | 2008-12-09 13:15:23 +0000 | [diff] [blame] | 109 | if (Alignment.isInvalid()) |
Daniel Dunbar | fcdd8fe | 2008-10-04 19:21:03 +0000 | [diff] [blame] | 110 | return; |
| 111 | |
| 112 | PP.Lex(Tok); |
| 113 | } else if (Tok.is(tok::identifier)) { |
| 114 | const IdentifierInfo *II = Tok.getIdentifierInfo(); |
Chris Lattner | 08631c5 | 2008-11-23 21:45:46 +0000 | [diff] [blame] | 115 | if (II->isStr("show")) { |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 116 | Kind = Sema::PPK_Show; |
Daniel Dunbar | fcdd8fe | 2008-10-04 19:21:03 +0000 | [diff] [blame] | 117 | PP.Lex(Tok); |
| 118 | } else { |
Chris Lattner | 08631c5 | 2008-11-23 21:45:46 +0000 | [diff] [blame] | 119 | if (II->isStr("push")) { |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 120 | Kind = Sema::PPK_Push; |
Chris Lattner | 08631c5 | 2008-11-23 21:45:46 +0000 | [diff] [blame] | 121 | } else if (II->isStr("pop")) { |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 122 | Kind = Sema::PPK_Pop; |
Daniel Dunbar | fcdd8fe | 2008-10-04 19:21:03 +0000 | [diff] [blame] | 123 | } else { |
| 124 | PP.Diag(Tok.getLocation(), diag::warn_pragma_pack_invalid_action); |
| 125 | return; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 126 | } |
Daniel Dunbar | fcdd8fe | 2008-10-04 19:21:03 +0000 | [diff] [blame] | 127 | PP.Lex(Tok); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 128 | |
Daniel Dunbar | fcdd8fe | 2008-10-04 19:21:03 +0000 | [diff] [blame] | 129 | if (Tok.is(tok::comma)) { |
| 130 | PP.Lex(Tok); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 131 | |
Daniel Dunbar | fcdd8fe | 2008-10-04 19:21:03 +0000 | [diff] [blame] | 132 | if (Tok.is(tok::numeric_constant)) { |
| 133 | Alignment = Actions.ActOnNumericConstant(Tok); |
Sebastian Redl | 0e9eabc | 2008-12-09 13:15:23 +0000 | [diff] [blame] | 134 | if (Alignment.isInvalid()) |
Daniel Dunbar | fcdd8fe | 2008-10-04 19:21:03 +0000 | [diff] [blame] | 135 | return; |
| 136 | |
| 137 | PP.Lex(Tok); |
| 138 | } else if (Tok.is(tok::identifier)) { |
| 139 | Name = Tok.getIdentifierInfo(); |
| 140 | PP.Lex(Tok); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 141 | |
Daniel Dunbar | fcdd8fe | 2008-10-04 19:21:03 +0000 | [diff] [blame] | 142 | if (Tok.is(tok::comma)) { |
| 143 | PP.Lex(Tok); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 144 | |
Daniel Dunbar | fcdd8fe | 2008-10-04 19:21:03 +0000 | [diff] [blame] | 145 | if (Tok.isNot(tok::numeric_constant)) { |
Chris Lattner | 08631c5 | 2008-11-23 21:45:46 +0000 | [diff] [blame] | 146 | PP.Diag(Tok.getLocation(), diag::warn_pragma_pack_malformed); |
Daniel Dunbar | fcdd8fe | 2008-10-04 19:21:03 +0000 | [diff] [blame] | 147 | return; |
| 148 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 149 | |
Daniel Dunbar | fcdd8fe | 2008-10-04 19:21:03 +0000 | [diff] [blame] | 150 | Alignment = Actions.ActOnNumericConstant(Tok); |
Sebastian Redl | 0e9eabc | 2008-12-09 13:15:23 +0000 | [diff] [blame] | 151 | if (Alignment.isInvalid()) |
Daniel Dunbar | fcdd8fe | 2008-10-04 19:21:03 +0000 | [diff] [blame] | 152 | return; |
| 153 | |
| 154 | PP.Lex(Tok); |
| 155 | } |
| 156 | } else { |
Chris Lattner | 08631c5 | 2008-11-23 21:45:46 +0000 | [diff] [blame] | 157 | PP.Diag(Tok.getLocation(), diag::warn_pragma_pack_malformed); |
Daniel Dunbar | fcdd8fe | 2008-10-04 19:21:03 +0000 | [diff] [blame] | 158 | return; |
| 159 | } |
| 160 | } |
| 161 | } |
Sebastian Redl | 0e9eabc | 2008-12-09 13:15:23 +0000 | [diff] [blame] | 162 | } |
Daniel Dunbar | fcdd8fe | 2008-10-04 19:21:03 +0000 | [diff] [blame] | 163 | |
| 164 | if (Tok.isNot(tok::r_paren)) { |
Ted Kremenek | 4726d03 | 2009-03-23 22:28:25 +0000 | [diff] [blame] | 165 | PP.Diag(Tok.getLocation(), diag::warn_pragma_expected_rparen) << "pack"; |
Daniel Dunbar | fcdd8fe | 2008-10-04 19:21:03 +0000 | [diff] [blame] | 166 | return; |
| 167 | } |
| 168 | |
Daniel Dunbar | 861800c | 2010-05-26 23:29:06 +0000 | [diff] [blame] | 169 | SourceLocation RParenLoc = Tok.getLocation(); |
Eli Friedman | 9991479 | 2009-06-05 00:49:58 +0000 | [diff] [blame] | 170 | PP.Lex(Tok); |
Peter Collingbourne | 8402155 | 2011-02-28 02:37:51 +0000 | [diff] [blame] | 171 | if (Tok.isNot(tok::eod)) { |
Eli Friedman | 9991479 | 2009-06-05 00:49:58 +0000 | [diff] [blame] | 172 | PP.Diag(Tok.getLocation(), diag::warn_pragma_extra_tokens_at_eol) << "pack"; |
| 173 | return; |
| 174 | } |
| 175 | |
Sebastian Redl | effa8d1 | 2008-12-10 00:02:53 +0000 | [diff] [blame] | 176 | Actions.ActOnPragmaPack(Kind, Name, Alignment.release(), PackLoc, |
Daniel Dunbar | fcdd8fe | 2008-10-04 19:21:03 +0000 | [diff] [blame] | 177 | LParenLoc, RParenLoc); |
| 178 | } |
| 179 | |
Fariborz Jahanian | 62c9258 | 2011-04-25 18:49:15 +0000 | [diff] [blame] | 180 | // #pragma ms_struct on |
| 181 | // #pragma ms_struct off |
| 182 | void PragmaMSStructHandler::HandlePragma(Preprocessor &PP, |
| 183 | PragmaIntroducerKind Introducer, |
| 184 | Token &MSStructTok) { |
| 185 | Sema::PragmaMSStructKind Kind = Sema::PMSST_OFF; |
| 186 | |
| 187 | Token Tok; |
| 188 | PP.Lex(Tok); |
| 189 | if (Tok.isNot(tok::identifier)) { |
| 190 | PP.Diag(Tok.getLocation(), diag::warn_pragma_ms_struct); |
| 191 | return; |
| 192 | } |
| 193 | const IdentifierInfo *II = Tok.getIdentifierInfo(); |
| 194 | if (II->isStr("on")) { |
| 195 | Kind = Sema::PMSST_ON; |
| 196 | PP.Lex(Tok); |
| 197 | } |
| 198 | else if (II->isStr("off") || II->isStr("reset")) |
| 199 | PP.Lex(Tok); |
| 200 | else { |
| 201 | PP.Diag(Tok.getLocation(), diag::warn_pragma_ms_struct); |
| 202 | return; |
| 203 | } |
| 204 | |
| 205 | if (Tok.isNot(tok::eod)) { |
| 206 | PP.Diag(Tok.getLocation(), diag::warn_pragma_extra_tokens_at_eol) << "ms_struct"; |
| 207 | return; |
| 208 | } |
| 209 | Actions.ActOnPragmaMSStruct(Kind); |
| 210 | } |
| 211 | |
Daniel Dunbar | cbb98ed | 2010-07-31 19:17:07 +0000 | [diff] [blame] | 212 | // #pragma 'align' '=' {'native','natural','mac68k','power','reset'} |
| 213 | // #pragma 'options 'align' '=' {'native','natural','mac68k','power','reset'} |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 214 | static void ParseAlignPragma(Sema &Actions, Preprocessor &PP, Token &FirstTok, |
Daniel Dunbar | cbb98ed | 2010-07-31 19:17:07 +0000 | [diff] [blame] | 215 | bool IsOptions) { |
Daniel Dunbar | 861800c | 2010-05-26 23:29:06 +0000 | [diff] [blame] | 216 | Token Tok; |
Daniel Dunbar | cbb98ed | 2010-07-31 19:17:07 +0000 | [diff] [blame] | 217 | |
| 218 | if (IsOptions) { |
| 219 | PP.Lex(Tok); |
| 220 | if (Tok.isNot(tok::identifier) || |
| 221 | !Tok.getIdentifierInfo()->isStr("align")) { |
| 222 | PP.Diag(Tok.getLocation(), diag::warn_pragma_options_expected_align); |
| 223 | return; |
| 224 | } |
Daniel Dunbar | 861800c | 2010-05-26 23:29:06 +0000 | [diff] [blame] | 225 | } |
Daniel Dunbar | 638e7cf | 2010-05-27 18:42:09 +0000 | [diff] [blame] | 226 | |
Daniel Dunbar | 861800c | 2010-05-26 23:29:06 +0000 | [diff] [blame] | 227 | PP.Lex(Tok); |
| 228 | if (Tok.isNot(tok::equal)) { |
Daniel Dunbar | cbb98ed | 2010-07-31 19:17:07 +0000 | [diff] [blame] | 229 | PP.Diag(Tok.getLocation(), diag::warn_pragma_align_expected_equal) |
| 230 | << IsOptions; |
Daniel Dunbar | 861800c | 2010-05-26 23:29:06 +0000 | [diff] [blame] | 231 | return; |
| 232 | } |
| 233 | |
| 234 | PP.Lex(Tok); |
| 235 | if (Tok.isNot(tok::identifier)) { |
| 236 | PP.Diag(Tok.getLocation(), diag::warn_pragma_expected_identifier) |
Daniel Dunbar | cbb98ed | 2010-07-31 19:17:07 +0000 | [diff] [blame] | 237 | << (IsOptions ? "options" : "align"); |
Daniel Dunbar | 861800c | 2010-05-26 23:29:06 +0000 | [diff] [blame] | 238 | return; |
| 239 | } |
| 240 | |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 241 | Sema::PragmaOptionsAlignKind Kind = Sema::POAK_Natural; |
Daniel Dunbar | 861800c | 2010-05-26 23:29:06 +0000 | [diff] [blame] | 242 | const IdentifierInfo *II = Tok.getIdentifierInfo(); |
Daniel Dunbar | 638e7cf | 2010-05-27 18:42:09 +0000 | [diff] [blame] | 243 | if (II->isStr("native")) |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 244 | Kind = Sema::POAK_Native; |
Daniel Dunbar | 638e7cf | 2010-05-27 18:42:09 +0000 | [diff] [blame] | 245 | else if (II->isStr("natural")) |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 246 | Kind = Sema::POAK_Natural; |
Daniel Dunbar | 6f73914 | 2010-05-27 18:42:17 +0000 | [diff] [blame] | 247 | else if (II->isStr("packed")) |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 248 | Kind = Sema::POAK_Packed; |
Daniel Dunbar | 861800c | 2010-05-26 23:29:06 +0000 | [diff] [blame] | 249 | else if (II->isStr("power")) |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 250 | Kind = Sema::POAK_Power; |
Daniel Dunbar | 861800c | 2010-05-26 23:29:06 +0000 | [diff] [blame] | 251 | else if (II->isStr("mac68k")) |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 252 | Kind = Sema::POAK_Mac68k; |
Daniel Dunbar | 861800c | 2010-05-26 23:29:06 +0000 | [diff] [blame] | 253 | else if (II->isStr("reset")) |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 254 | Kind = Sema::POAK_Reset; |
Daniel Dunbar | 861800c | 2010-05-26 23:29:06 +0000 | [diff] [blame] | 255 | else { |
Daniel Dunbar | cbb98ed | 2010-07-31 19:17:07 +0000 | [diff] [blame] | 256 | PP.Diag(Tok.getLocation(), diag::warn_pragma_align_invalid_option) |
| 257 | << IsOptions; |
Daniel Dunbar | 861800c | 2010-05-26 23:29:06 +0000 | [diff] [blame] | 258 | return; |
| 259 | } |
| 260 | |
| 261 | SourceLocation KindLoc = Tok.getLocation(); |
| 262 | PP.Lex(Tok); |
Peter Collingbourne | 8402155 | 2011-02-28 02:37:51 +0000 | [diff] [blame] | 263 | if (Tok.isNot(tok::eod)) { |
Daniel Dunbar | 861800c | 2010-05-26 23:29:06 +0000 | [diff] [blame] | 264 | PP.Diag(Tok.getLocation(), diag::warn_pragma_extra_tokens_at_eol) |
Daniel Dunbar | cbb98ed | 2010-07-31 19:17:07 +0000 | [diff] [blame] | 265 | << (IsOptions ? "options" : "align"); |
Daniel Dunbar | 861800c | 2010-05-26 23:29:06 +0000 | [diff] [blame] | 266 | return; |
| 267 | } |
| 268 | |
Daniel Dunbar | cbb98ed | 2010-07-31 19:17:07 +0000 | [diff] [blame] | 269 | Actions.ActOnPragmaOptionsAlign(Kind, FirstTok.getLocation(), KindLoc); |
| 270 | } |
| 271 | |
Douglas Gregor | 80c60f7 | 2010-09-09 22:45:38 +0000 | [diff] [blame] | 272 | void PragmaAlignHandler::HandlePragma(Preprocessor &PP, |
| 273 | PragmaIntroducerKind Introducer, |
| 274 | Token &AlignTok) { |
Daniel Dunbar | cbb98ed | 2010-07-31 19:17:07 +0000 | [diff] [blame] | 275 | ParseAlignPragma(Actions, PP, AlignTok, /*IsOptions=*/false); |
| 276 | } |
| 277 | |
Douglas Gregor | 80c60f7 | 2010-09-09 22:45:38 +0000 | [diff] [blame] | 278 | void PragmaOptionsHandler::HandlePragma(Preprocessor &PP, |
| 279 | PragmaIntroducerKind Introducer, |
| 280 | Token &OptionsTok) { |
Daniel Dunbar | cbb98ed | 2010-07-31 19:17:07 +0000 | [diff] [blame] | 281 | ParseAlignPragma(Actions, PP, OptionsTok, /*IsOptions=*/true); |
Daniel Dunbar | 861800c | 2010-05-26 23:29:06 +0000 | [diff] [blame] | 282 | } |
| 283 | |
Ted Kremenek | 4726d03 | 2009-03-23 22:28:25 +0000 | [diff] [blame] | 284 | // #pragma unused(identifier) |
Douglas Gregor | 80c60f7 | 2010-09-09 22:45:38 +0000 | [diff] [blame] | 285 | void PragmaUnusedHandler::HandlePragma(Preprocessor &PP, |
| 286 | PragmaIntroducerKind Introducer, |
| 287 | Token &UnusedTok) { |
Ted Kremenek | 4726d03 | 2009-03-23 22:28:25 +0000 | [diff] [blame] | 288 | // FIXME: Should we be expanding macros here? My guess is no. |
| 289 | SourceLocation UnusedLoc = UnusedTok.getLocation(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 290 | |
Ted Kremenek | 4726d03 | 2009-03-23 22:28:25 +0000 | [diff] [blame] | 291 | // Lex the left '('. |
| 292 | Token Tok; |
| 293 | PP.Lex(Tok); |
| 294 | if (Tok.isNot(tok::l_paren)) { |
| 295 | PP.Diag(Tok.getLocation(), diag::warn_pragma_expected_lparen) << "unused"; |
| 296 | return; |
| 297 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 298 | |
Ted Kremenek | 4726d03 | 2009-03-23 22:28:25 +0000 | [diff] [blame] | 299 | // Lex the declaration reference(s). |
Ted Kremenek | 7a02a37 | 2009-08-03 23:24:57 +0000 | [diff] [blame] | 300 | llvm::SmallVector<Token, 5> Identifiers; |
Ted Kremenek | 4726d03 | 2009-03-23 22:28:25 +0000 | [diff] [blame] | 301 | SourceLocation RParenLoc; |
| 302 | bool LexID = true; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 303 | |
Ted Kremenek | 4726d03 | 2009-03-23 22:28:25 +0000 | [diff] [blame] | 304 | while (true) { |
| 305 | PP.Lex(Tok); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 306 | |
Ted Kremenek | 4726d03 | 2009-03-23 22:28:25 +0000 | [diff] [blame] | 307 | if (LexID) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 308 | if (Tok.is(tok::identifier)) { |
Ted Kremenek | 7a02a37 | 2009-08-03 23:24:57 +0000 | [diff] [blame] | 309 | Identifiers.push_back(Tok); |
Ted Kremenek | 4726d03 | 2009-03-23 22:28:25 +0000 | [diff] [blame] | 310 | LexID = false; |
| 311 | continue; |
| 312 | } |
| 313 | |
Ted Kremenek | 7a02a37 | 2009-08-03 23:24:57 +0000 | [diff] [blame] | 314 | // Illegal token! |
Ted Kremenek | 4726d03 | 2009-03-23 22:28:25 +0000 | [diff] [blame] | 315 | PP.Diag(Tok.getLocation(), diag::warn_pragma_unused_expected_var); |
| 316 | return; |
| 317 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 318 | |
Ted Kremenek | 4726d03 | 2009-03-23 22:28:25 +0000 | [diff] [blame] | 319 | // We are execting a ')' or a ','. |
| 320 | if (Tok.is(tok::comma)) { |
| 321 | LexID = true; |
| 322 | continue; |
| 323 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 324 | |
Ted Kremenek | 4726d03 | 2009-03-23 22:28:25 +0000 | [diff] [blame] | 325 | if (Tok.is(tok::r_paren)) { |
| 326 | RParenLoc = Tok.getLocation(); |
| 327 | break; |
| 328 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 329 | |
Ted Kremenek | 7a02a37 | 2009-08-03 23:24:57 +0000 | [diff] [blame] | 330 | // Illegal token! |
Ted Kremenek | 4726d03 | 2009-03-23 22:28:25 +0000 | [diff] [blame] | 331 | PP.Diag(Tok.getLocation(), diag::warn_pragma_unused_expected_punc); |
| 332 | return; |
| 333 | } |
Eli Friedman | 9991479 | 2009-06-05 00:49:58 +0000 | [diff] [blame] | 334 | |
| 335 | PP.Lex(Tok); |
Peter Collingbourne | 8402155 | 2011-02-28 02:37:51 +0000 | [diff] [blame] | 336 | if (Tok.isNot(tok::eod)) { |
Eli Friedman | 9991479 | 2009-06-05 00:49:58 +0000 | [diff] [blame] | 337 | PP.Diag(Tok.getLocation(), diag::warn_pragma_extra_tokens_at_eol) << |
| 338 | "unused"; |
| 339 | return; |
| 340 | } |
| 341 | |
Ted Kremenek | 4726d03 | 2009-03-23 22:28:25 +0000 | [diff] [blame] | 342 | // Verify that we have a location for the right parenthesis. |
| 343 | assert(RParenLoc.isValid() && "Valid '#pragma unused' must have ')'"); |
Ted Kremenek | 7a02a37 | 2009-08-03 23:24:57 +0000 | [diff] [blame] | 344 | assert(!Identifiers.empty() && "Valid '#pragma unused' must have arguments"); |
Ted Kremenek | 4726d03 | 2009-03-23 22:28:25 +0000 | [diff] [blame] | 345 | |
Argyrios Kyrtzidis | b918d0f | 2011-01-17 18:58:44 +0000 | [diff] [blame] | 346 | // For each identifier token, insert into the token stream a |
| 347 | // annot_pragma_unused token followed by the identifier token. |
| 348 | // This allows us to cache a "#pragma unused" that occurs inside an inline |
| 349 | // C++ member function. |
| 350 | |
| 351 | Token *Toks = new Token[2*Identifiers.size()]; |
| 352 | for (unsigned i=0; i != Identifiers.size(); i++) { |
| 353 | Token &pragmaUnusedTok = Toks[2*i], &idTok = Toks[2*i+1]; |
| 354 | pragmaUnusedTok.startToken(); |
| 355 | pragmaUnusedTok.setKind(tok::annot_pragma_unused); |
| 356 | pragmaUnusedTok.setLocation(UnusedLoc); |
| 357 | idTok = Identifiers[i]; |
| 358 | } |
| 359 | PP.EnterTokenStream(Toks, 2*Identifiers.size(), /*DisableMacroExpansion=*/true, /*OwnsTokens=*/true); |
Ted Kremenek | 4726d03 | 2009-03-23 22:28:25 +0000 | [diff] [blame] | 360 | } |
Eli Friedman | 9991479 | 2009-06-05 00:49:58 +0000 | [diff] [blame] | 361 | |
| 362 | // #pragma weak identifier |
| 363 | // #pragma weak identifier '=' identifier |
Douglas Gregor | 80c60f7 | 2010-09-09 22:45:38 +0000 | [diff] [blame] | 364 | void PragmaWeakHandler::HandlePragma(Preprocessor &PP, |
| 365 | PragmaIntroducerKind Introducer, |
| 366 | Token &WeakTok) { |
Eli Friedman | 9991479 | 2009-06-05 00:49:58 +0000 | [diff] [blame] | 367 | // FIXME: Should we be expanding macros here? My guess is no. |
| 368 | SourceLocation WeakLoc = WeakTok.getLocation(); |
| 369 | |
| 370 | Token Tok; |
| 371 | PP.Lex(Tok); |
| 372 | if (Tok.isNot(tok::identifier)) { |
| 373 | PP.Diag(Tok.getLocation(), diag::warn_pragma_expected_identifier) << "weak"; |
| 374 | return; |
| 375 | } |
| 376 | |
| 377 | IdentifierInfo *WeakName = Tok.getIdentifierInfo(), *AliasName = 0; |
| 378 | SourceLocation WeakNameLoc = Tok.getLocation(), AliasNameLoc; |
| 379 | |
| 380 | PP.Lex(Tok); |
| 381 | if (Tok.is(tok::equal)) { |
| 382 | PP.Lex(Tok); |
| 383 | if (Tok.isNot(tok::identifier)) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 384 | PP.Diag(Tok.getLocation(), diag::warn_pragma_expected_identifier) |
Eli Friedman | 9991479 | 2009-06-05 00:49:58 +0000 | [diff] [blame] | 385 | << "weak"; |
| 386 | return; |
| 387 | } |
| 388 | AliasName = Tok.getIdentifierInfo(); |
| 389 | AliasNameLoc = Tok.getLocation(); |
| 390 | PP.Lex(Tok); |
| 391 | } |
| 392 | |
Peter Collingbourne | 8402155 | 2011-02-28 02:37:51 +0000 | [diff] [blame] | 393 | if (Tok.isNot(tok::eod)) { |
Eli Friedman | 9991479 | 2009-06-05 00:49:58 +0000 | [diff] [blame] | 394 | PP.Diag(Tok.getLocation(), diag::warn_pragma_extra_tokens_at_eol) << "weak"; |
| 395 | return; |
| 396 | } |
| 397 | |
| 398 | if (AliasName) { |
| 399 | Actions.ActOnPragmaWeakAlias(WeakName, AliasName, WeakLoc, WeakNameLoc, |
| 400 | AliasNameLoc); |
| 401 | } else { |
| 402 | Actions.ActOnPragmaWeakID(WeakName, WeakLoc, WeakNameLoc); |
| 403 | } |
| 404 | } |
Peter Collingbourne | 321b817 | 2011-02-14 01:42:35 +0000 | [diff] [blame] | 405 | |
| 406 | void |
| 407 | PragmaFPContractHandler::HandlePragma(Preprocessor &PP, |
| 408 | PragmaIntroducerKind Introducer, |
| 409 | Token &Tok) { |
| 410 | tok::OnOffSwitch OOS; |
| 411 | if (PP.LexOnOffSwitch(OOS)) |
| 412 | return; |
| 413 | |
| 414 | Actions.ActOnPragmaFPContract(OOS); |
| 415 | } |
Peter Collingbourne | f315fa8 | 2011-02-14 01:42:53 +0000 | [diff] [blame] | 416 | |
| 417 | void |
| 418 | PragmaOpenCLExtensionHandler::HandlePragma(Preprocessor &PP, |
| 419 | PragmaIntroducerKind Introducer, |
| 420 | Token &Tok) { |
Tanya Lattner | b38b6a7 | 2011-04-14 23:35:31 +0000 | [diff] [blame] | 421 | PP.LexUnexpandedToken(Tok); |
Peter Collingbourne | f315fa8 | 2011-02-14 01:42:53 +0000 | [diff] [blame] | 422 | if (Tok.isNot(tok::identifier)) { |
| 423 | PP.Diag(Tok.getLocation(), diag::warn_pragma_expected_identifier) << |
| 424 | "OPENCL"; |
| 425 | return; |
| 426 | } |
| 427 | IdentifierInfo *ename = Tok.getIdentifierInfo(); |
| 428 | SourceLocation NameLoc = Tok.getLocation(); |
| 429 | |
| 430 | PP.Lex(Tok); |
| 431 | if (Tok.isNot(tok::colon)) { |
| 432 | PP.Diag(Tok.getLocation(), diag::warn_pragma_expected_colon) << ename; |
| 433 | return; |
| 434 | } |
| 435 | |
| 436 | PP.Lex(Tok); |
| 437 | if (Tok.isNot(tok::identifier)) { |
| 438 | PP.Diag(Tok.getLocation(), diag::warn_pragma_expected_enable_disable); |
| 439 | return; |
| 440 | } |
| 441 | IdentifierInfo *op = Tok.getIdentifierInfo(); |
| 442 | |
| 443 | unsigned state; |
| 444 | if (op->isStr("enable")) { |
| 445 | state = 1; |
| 446 | } else if (op->isStr("disable")) { |
| 447 | state = 0; |
| 448 | } else { |
| 449 | PP.Diag(Tok.getLocation(), diag::warn_pragma_expected_enable_disable); |
| 450 | return; |
| 451 | } |
| 452 | |
| 453 | OpenCLOptions &f = Actions.getOpenCLOptions(); |
| 454 | if (ename->isStr("all")) { |
| 455 | #define OPENCLEXT(nm) f.nm = state; |
| 456 | #include "clang/Basic/OpenCLExtensions.def" |
| 457 | } |
| 458 | #define OPENCLEXT(nm) else if (ename->isStr(#nm)) { f.nm = state; } |
| 459 | #include "clang/Basic/OpenCLExtensions.def" |
| 460 | else { |
| 461 | PP.Diag(NameLoc, diag::warn_pragma_unknown_extension) << ename; |
| 462 | return; |
| 463 | } |
| 464 | } |
| 465 | |