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