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