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