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