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