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