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