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