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