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 | |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame] | 14 | #include "RAIIObjectsForParser.h" |
Stephen Hines | 176edba | 2014-12-01 14:53:08 -0800 | [diff] [blame] | 15 | #include "clang/AST/ASTContext.h" |
| 16 | #include "clang/Basic/TargetInfo.h" |
Chandler Carruth | 55fc873 | 2012-12-04 09:13:33 +0000 | [diff] [blame] | 17 | #include "clang/Lex/Preprocessor.h" |
Chris Lattner | 500d329 | 2009-01-29 05:15:15 +0000 | [diff] [blame] | 18 | #include "clang/Parse/ParseDiagnostic.h" |
Ted Kremenek | 4726d03 | 2009-03-23 22:28:25 +0000 | [diff] [blame] | 19 | #include "clang/Parse/Parser.h" |
Stephen Hines | c568f1e | 2014-07-21 00:47:37 -0700 | [diff] [blame] | 20 | #include "clang/Sema/LoopHint.h" |
Tareq A. Siraj | 6afcf88 | 2013-04-16 19:37:38 +0000 | [diff] [blame] | 21 | #include "clang/Sema/Scope.h" |
Reid Kleckner | 7adf79a | 2013-05-06 21:02:12 +0000 | [diff] [blame] | 22 | #include "llvm/ADT/StringSwitch.h" |
Daniel Dunbar | fcdd8fe | 2008-10-04 19:21:03 +0000 | [diff] [blame] | 23 | using namespace clang; |
| 24 | |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 25 | namespace { |
| 26 | |
| 27 | struct PragmaAlignHandler : public PragmaHandler { |
| 28 | explicit PragmaAlignHandler() : PragmaHandler("align") {} |
| 29 | void HandlePragma(Preprocessor &PP, PragmaIntroducerKind Introducer, |
| 30 | Token &FirstToken) override; |
| 31 | }; |
| 32 | |
| 33 | struct PragmaGCCVisibilityHandler : public PragmaHandler { |
| 34 | explicit PragmaGCCVisibilityHandler() : PragmaHandler("visibility") {} |
| 35 | void HandlePragma(Preprocessor &PP, PragmaIntroducerKind Introducer, |
| 36 | Token &FirstToken) override; |
| 37 | }; |
| 38 | |
| 39 | struct PragmaOptionsHandler : public PragmaHandler { |
| 40 | explicit PragmaOptionsHandler() : PragmaHandler("options") {} |
| 41 | void HandlePragma(Preprocessor &PP, PragmaIntroducerKind Introducer, |
| 42 | Token &FirstToken) override; |
| 43 | }; |
| 44 | |
| 45 | struct PragmaPackHandler : public PragmaHandler { |
| 46 | explicit PragmaPackHandler() : PragmaHandler("pack") {} |
| 47 | void HandlePragma(Preprocessor &PP, PragmaIntroducerKind Introducer, |
| 48 | Token &FirstToken) override; |
| 49 | }; |
| 50 | |
| 51 | struct PragmaMSStructHandler : public PragmaHandler { |
| 52 | explicit PragmaMSStructHandler() : PragmaHandler("ms_struct") {} |
| 53 | void HandlePragma(Preprocessor &PP, PragmaIntroducerKind Introducer, |
| 54 | Token &FirstToken) override; |
| 55 | }; |
| 56 | |
| 57 | struct PragmaUnusedHandler : public PragmaHandler { |
| 58 | PragmaUnusedHandler() : PragmaHandler("unused") {} |
| 59 | void HandlePragma(Preprocessor &PP, PragmaIntroducerKind Introducer, |
| 60 | Token &FirstToken) override; |
| 61 | }; |
| 62 | |
| 63 | struct PragmaWeakHandler : public PragmaHandler { |
| 64 | explicit PragmaWeakHandler() : PragmaHandler("weak") {} |
| 65 | void HandlePragma(Preprocessor &PP, PragmaIntroducerKind Introducer, |
| 66 | Token &FirstToken) override; |
| 67 | }; |
| 68 | |
| 69 | struct PragmaRedefineExtnameHandler : public PragmaHandler { |
| 70 | explicit PragmaRedefineExtnameHandler() : PragmaHandler("redefine_extname") {} |
| 71 | void HandlePragma(Preprocessor &PP, PragmaIntroducerKind Introducer, |
| 72 | Token &FirstToken) override; |
| 73 | }; |
| 74 | |
| 75 | struct PragmaOpenCLExtensionHandler : public PragmaHandler { |
| 76 | PragmaOpenCLExtensionHandler() : PragmaHandler("EXTENSION") {} |
| 77 | void HandlePragma(Preprocessor &PP, PragmaIntroducerKind Introducer, |
| 78 | Token &FirstToken) override; |
| 79 | }; |
| 80 | |
| 81 | |
| 82 | struct PragmaFPContractHandler : public PragmaHandler { |
| 83 | PragmaFPContractHandler() : PragmaHandler("FP_CONTRACT") {} |
| 84 | void HandlePragma(Preprocessor &PP, PragmaIntroducerKind Introducer, |
| 85 | Token &FirstToken) override; |
| 86 | }; |
| 87 | |
| 88 | struct PragmaNoOpenMPHandler : public PragmaHandler { |
| 89 | PragmaNoOpenMPHandler() : PragmaHandler("omp") { } |
| 90 | void HandlePragma(Preprocessor &PP, PragmaIntroducerKind Introducer, |
| 91 | Token &FirstToken) override; |
| 92 | }; |
| 93 | |
| 94 | struct PragmaOpenMPHandler : public PragmaHandler { |
| 95 | PragmaOpenMPHandler() : PragmaHandler("omp") { } |
| 96 | void HandlePragma(Preprocessor &PP, PragmaIntroducerKind Introducer, |
| 97 | Token &FirstToken) override; |
| 98 | }; |
| 99 | |
| 100 | /// PragmaCommentHandler - "\#pragma comment ...". |
| 101 | struct PragmaCommentHandler : public PragmaHandler { |
| 102 | PragmaCommentHandler(Sema &Actions) |
| 103 | : PragmaHandler("comment"), Actions(Actions) {} |
| 104 | void HandlePragma(Preprocessor &PP, PragmaIntroducerKind Introducer, |
| 105 | Token &FirstToken) override; |
| 106 | private: |
| 107 | Sema &Actions; |
| 108 | }; |
| 109 | |
| 110 | struct PragmaDetectMismatchHandler : public PragmaHandler { |
| 111 | PragmaDetectMismatchHandler(Sema &Actions) |
| 112 | : PragmaHandler("detect_mismatch"), Actions(Actions) {} |
| 113 | void HandlePragma(Preprocessor &PP, PragmaIntroducerKind Introducer, |
| 114 | Token &FirstToken) override; |
| 115 | private: |
| 116 | Sema &Actions; |
| 117 | }; |
| 118 | |
| 119 | struct PragmaMSPointersToMembers : public PragmaHandler { |
| 120 | explicit PragmaMSPointersToMembers() : PragmaHandler("pointers_to_members") {} |
| 121 | void HandlePragma(Preprocessor &PP, PragmaIntroducerKind Introducer, |
| 122 | Token &FirstToken) override; |
| 123 | }; |
| 124 | |
| 125 | struct PragmaMSVtorDisp : public PragmaHandler { |
| 126 | explicit PragmaMSVtorDisp() : PragmaHandler("vtordisp") {} |
| 127 | void HandlePragma(Preprocessor &PP, PragmaIntroducerKind Introducer, |
| 128 | Token &FirstToken) override; |
| 129 | }; |
| 130 | |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame] | 131 | struct PragmaMSPragma : public PragmaHandler { |
| 132 | explicit PragmaMSPragma(const char *name) : PragmaHandler(name) {} |
| 133 | void HandlePragma(Preprocessor &PP, PragmaIntroducerKind Introducer, |
| 134 | Token &FirstToken) override; |
| 135 | }; |
| 136 | |
| 137 | /// PragmaOptimizeHandler - "\#pragma clang optimize on/off". |
| 138 | struct PragmaOptimizeHandler : public PragmaHandler { |
| 139 | PragmaOptimizeHandler(Sema &S) |
| 140 | : PragmaHandler("optimize"), Actions(S) {} |
| 141 | void HandlePragma(Preprocessor &PP, PragmaIntroducerKind Introducer, |
| 142 | Token &FirstToken) override; |
| 143 | private: |
| 144 | Sema &Actions; |
| 145 | }; |
| 146 | |
Stephen Hines | c568f1e | 2014-07-21 00:47:37 -0700 | [diff] [blame] | 147 | struct PragmaLoopHintHandler : public PragmaHandler { |
| 148 | PragmaLoopHintHandler() : PragmaHandler("loop") {} |
| 149 | void HandlePragma(Preprocessor &PP, PragmaIntroducerKind Introducer, |
| 150 | Token &FirstToken) override; |
| 151 | }; |
| 152 | |
Stephen Hines | 176edba | 2014-12-01 14:53:08 -0800 | [diff] [blame] | 153 | struct PragmaUnrollHintHandler : public PragmaHandler { |
| 154 | PragmaUnrollHintHandler(const char *name) : PragmaHandler(name) {} |
| 155 | void HandlePragma(Preprocessor &PP, PragmaIntroducerKind Introducer, |
| 156 | Token &FirstToken) override; |
| 157 | }; |
| 158 | |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 159 | } // end namespace |
| 160 | |
| 161 | void Parser::initializePragmaHandlers() { |
| 162 | AlignHandler.reset(new PragmaAlignHandler()); |
| 163 | PP.AddPragmaHandler(AlignHandler.get()); |
| 164 | |
| 165 | GCCVisibilityHandler.reset(new PragmaGCCVisibilityHandler()); |
| 166 | PP.AddPragmaHandler("GCC", GCCVisibilityHandler.get()); |
| 167 | |
| 168 | OptionsHandler.reset(new PragmaOptionsHandler()); |
| 169 | PP.AddPragmaHandler(OptionsHandler.get()); |
| 170 | |
| 171 | PackHandler.reset(new PragmaPackHandler()); |
| 172 | PP.AddPragmaHandler(PackHandler.get()); |
| 173 | |
| 174 | MSStructHandler.reset(new PragmaMSStructHandler()); |
| 175 | PP.AddPragmaHandler(MSStructHandler.get()); |
| 176 | |
| 177 | UnusedHandler.reset(new PragmaUnusedHandler()); |
| 178 | PP.AddPragmaHandler(UnusedHandler.get()); |
| 179 | |
| 180 | WeakHandler.reset(new PragmaWeakHandler()); |
| 181 | PP.AddPragmaHandler(WeakHandler.get()); |
| 182 | |
| 183 | RedefineExtnameHandler.reset(new PragmaRedefineExtnameHandler()); |
| 184 | PP.AddPragmaHandler(RedefineExtnameHandler.get()); |
| 185 | |
| 186 | FPContractHandler.reset(new PragmaFPContractHandler()); |
| 187 | PP.AddPragmaHandler("STDC", FPContractHandler.get()); |
| 188 | |
| 189 | if (getLangOpts().OpenCL) { |
| 190 | OpenCLExtensionHandler.reset(new PragmaOpenCLExtensionHandler()); |
| 191 | PP.AddPragmaHandler("OPENCL", OpenCLExtensionHandler.get()); |
| 192 | |
| 193 | PP.AddPragmaHandler("OPENCL", FPContractHandler.get()); |
| 194 | } |
| 195 | if (getLangOpts().OpenMP) |
| 196 | OpenMPHandler.reset(new PragmaOpenMPHandler()); |
| 197 | else |
| 198 | OpenMPHandler.reset(new PragmaNoOpenMPHandler()); |
| 199 | PP.AddPragmaHandler(OpenMPHandler.get()); |
| 200 | |
| 201 | if (getLangOpts().MicrosoftExt) { |
| 202 | MSCommentHandler.reset(new PragmaCommentHandler(Actions)); |
| 203 | PP.AddPragmaHandler(MSCommentHandler.get()); |
| 204 | MSDetectMismatchHandler.reset(new PragmaDetectMismatchHandler(Actions)); |
| 205 | PP.AddPragmaHandler(MSDetectMismatchHandler.get()); |
| 206 | MSPointersToMembers.reset(new PragmaMSPointersToMembers()); |
| 207 | PP.AddPragmaHandler(MSPointersToMembers.get()); |
| 208 | MSVtorDisp.reset(new PragmaMSVtorDisp()); |
| 209 | PP.AddPragmaHandler(MSVtorDisp.get()); |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame] | 210 | MSInitSeg.reset(new PragmaMSPragma("init_seg")); |
| 211 | PP.AddPragmaHandler(MSInitSeg.get()); |
| 212 | MSDataSeg.reset(new PragmaMSPragma("data_seg")); |
| 213 | PP.AddPragmaHandler(MSDataSeg.get()); |
| 214 | MSBSSSeg.reset(new PragmaMSPragma("bss_seg")); |
| 215 | PP.AddPragmaHandler(MSBSSSeg.get()); |
| 216 | MSConstSeg.reset(new PragmaMSPragma("const_seg")); |
| 217 | PP.AddPragmaHandler(MSConstSeg.get()); |
| 218 | MSCodeSeg.reset(new PragmaMSPragma("code_seg")); |
| 219 | PP.AddPragmaHandler(MSCodeSeg.get()); |
| 220 | MSSection.reset(new PragmaMSPragma("section")); |
| 221 | PP.AddPragmaHandler(MSSection.get()); |
Stephen Hines | 0e2c34f | 2015-03-23 12:09:02 -0700 | [diff] [blame] | 222 | } else if (getTargetInfo().getTriple().isPS4()) { |
| 223 | MSCommentHandler.reset(new PragmaCommentHandler(Actions)); |
| 224 | PP.AddPragmaHandler(MSCommentHandler.get()); |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 225 | } |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame] | 226 | |
| 227 | OptimizeHandler.reset(new PragmaOptimizeHandler(Actions)); |
| 228 | PP.AddPragmaHandler("clang", OptimizeHandler.get()); |
Stephen Hines | c568f1e | 2014-07-21 00:47:37 -0700 | [diff] [blame] | 229 | |
| 230 | LoopHintHandler.reset(new PragmaLoopHintHandler()); |
| 231 | PP.AddPragmaHandler("clang", LoopHintHandler.get()); |
Stephen Hines | 176edba | 2014-12-01 14:53:08 -0800 | [diff] [blame] | 232 | |
| 233 | UnrollHintHandler.reset(new PragmaUnrollHintHandler("unroll")); |
| 234 | PP.AddPragmaHandler(UnrollHintHandler.get()); |
| 235 | |
| 236 | NoUnrollHintHandler.reset(new PragmaUnrollHintHandler("nounroll")); |
| 237 | PP.AddPragmaHandler(NoUnrollHintHandler.get()); |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 238 | } |
| 239 | |
| 240 | void Parser::resetPragmaHandlers() { |
| 241 | // Remove the pragma handlers we installed. |
| 242 | PP.RemovePragmaHandler(AlignHandler.get()); |
| 243 | AlignHandler.reset(); |
| 244 | PP.RemovePragmaHandler("GCC", GCCVisibilityHandler.get()); |
| 245 | GCCVisibilityHandler.reset(); |
| 246 | PP.RemovePragmaHandler(OptionsHandler.get()); |
| 247 | OptionsHandler.reset(); |
| 248 | PP.RemovePragmaHandler(PackHandler.get()); |
| 249 | PackHandler.reset(); |
| 250 | PP.RemovePragmaHandler(MSStructHandler.get()); |
| 251 | MSStructHandler.reset(); |
| 252 | PP.RemovePragmaHandler(UnusedHandler.get()); |
| 253 | UnusedHandler.reset(); |
| 254 | PP.RemovePragmaHandler(WeakHandler.get()); |
| 255 | WeakHandler.reset(); |
| 256 | PP.RemovePragmaHandler(RedefineExtnameHandler.get()); |
| 257 | RedefineExtnameHandler.reset(); |
| 258 | |
| 259 | if (getLangOpts().OpenCL) { |
| 260 | PP.RemovePragmaHandler("OPENCL", OpenCLExtensionHandler.get()); |
| 261 | OpenCLExtensionHandler.reset(); |
| 262 | PP.RemovePragmaHandler("OPENCL", FPContractHandler.get()); |
| 263 | } |
| 264 | PP.RemovePragmaHandler(OpenMPHandler.get()); |
| 265 | OpenMPHandler.reset(); |
| 266 | |
| 267 | if (getLangOpts().MicrosoftExt) { |
| 268 | PP.RemovePragmaHandler(MSCommentHandler.get()); |
| 269 | MSCommentHandler.reset(); |
| 270 | PP.RemovePragmaHandler(MSDetectMismatchHandler.get()); |
| 271 | MSDetectMismatchHandler.reset(); |
| 272 | PP.RemovePragmaHandler(MSPointersToMembers.get()); |
| 273 | MSPointersToMembers.reset(); |
| 274 | PP.RemovePragmaHandler(MSVtorDisp.get()); |
| 275 | MSVtorDisp.reset(); |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame] | 276 | PP.RemovePragmaHandler(MSInitSeg.get()); |
| 277 | MSInitSeg.reset(); |
| 278 | PP.RemovePragmaHandler(MSDataSeg.get()); |
| 279 | MSDataSeg.reset(); |
| 280 | PP.RemovePragmaHandler(MSBSSSeg.get()); |
| 281 | MSBSSSeg.reset(); |
| 282 | PP.RemovePragmaHandler(MSConstSeg.get()); |
| 283 | MSConstSeg.reset(); |
| 284 | PP.RemovePragmaHandler(MSCodeSeg.get()); |
| 285 | MSCodeSeg.reset(); |
| 286 | PP.RemovePragmaHandler(MSSection.get()); |
| 287 | MSSection.reset(); |
Stephen Hines | 0e2c34f | 2015-03-23 12:09:02 -0700 | [diff] [blame] | 288 | } else if (getTargetInfo().getTriple().isPS4()) { |
| 289 | PP.RemovePragmaHandler(MSCommentHandler.get()); |
| 290 | MSCommentHandler.reset(); |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 291 | } |
| 292 | |
| 293 | PP.RemovePragmaHandler("STDC", FPContractHandler.get()); |
| 294 | FPContractHandler.reset(); |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame] | 295 | |
| 296 | PP.RemovePragmaHandler("clang", OptimizeHandler.get()); |
| 297 | OptimizeHandler.reset(); |
Stephen Hines | c568f1e | 2014-07-21 00:47:37 -0700 | [diff] [blame] | 298 | |
| 299 | PP.RemovePragmaHandler("clang", LoopHintHandler.get()); |
| 300 | LoopHintHandler.reset(); |
Stephen Hines | 176edba | 2014-12-01 14:53:08 -0800 | [diff] [blame] | 301 | |
| 302 | PP.RemovePragmaHandler(UnrollHintHandler.get()); |
| 303 | UnrollHintHandler.reset(); |
| 304 | |
| 305 | PP.RemovePragmaHandler(NoUnrollHintHandler.get()); |
| 306 | NoUnrollHintHandler.reset(); |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 307 | } |
| 308 | |
Argyrios Kyrtzidis | b918d0f | 2011-01-17 18:58:44 +0000 | [diff] [blame] | 309 | /// \brief Handle the annotation token produced for #pragma unused(...) |
| 310 | /// |
| 311 | /// Each annot_pragma_unused is followed by the argument token so e.g. |
| 312 | /// "#pragma unused(x,y)" becomes: |
| 313 | /// annot_pragma_unused 'x' annot_pragma_unused 'y' |
| 314 | void Parser::HandlePragmaUnused() { |
| 315 | assert(Tok.is(tok::annot_pragma_unused)); |
| 316 | SourceLocation UnusedLoc = ConsumeToken(); |
| 317 | Actions.ActOnPragmaUnused(Tok, getCurScope(), UnusedLoc); |
| 318 | ConsumeToken(); // The argument token. |
| 319 | } |
Eli Friedman | aa8b0d1 | 2010-08-05 06:57:20 +0000 | [diff] [blame] | 320 | |
Rafael Espindola | 426fc94 | 2012-01-26 02:02:57 +0000 | [diff] [blame] | 321 | void Parser::HandlePragmaVisibility() { |
| 322 | assert(Tok.is(tok::annot_pragma_vis)); |
| 323 | const IdentifierInfo *VisType = |
| 324 | static_cast<IdentifierInfo *>(Tok.getAnnotationValue()); |
| 325 | SourceLocation VisLoc = ConsumeToken(); |
| 326 | Actions.ActOnPragmaVisibility(VisType, VisLoc); |
| 327 | } |
| 328 | |
Eli Friedman | aa5ab26 | 2012-02-23 23:47:16 +0000 | [diff] [blame] | 329 | struct PragmaPackInfo { |
| 330 | Sema::PragmaPackKind Kind; |
| 331 | IdentifierInfo *Name; |
Eli Friedman | 9595c7e | 2012-10-04 02:36:51 +0000 | [diff] [blame] | 332 | Token Alignment; |
Eli Friedman | aa5ab26 | 2012-02-23 23:47:16 +0000 | [diff] [blame] | 333 | SourceLocation LParenLoc; |
| 334 | SourceLocation RParenLoc; |
| 335 | }; |
| 336 | |
| 337 | void Parser::HandlePragmaPack() { |
| 338 | assert(Tok.is(tok::annot_pragma_pack)); |
| 339 | PragmaPackInfo *Info = |
| 340 | static_cast<PragmaPackInfo *>(Tok.getAnnotationValue()); |
| 341 | SourceLocation PragmaLoc = ConsumeToken(); |
Eli Friedman | 9595c7e | 2012-10-04 02:36:51 +0000 | [diff] [blame] | 342 | ExprResult Alignment; |
| 343 | if (Info->Alignment.is(tok::numeric_constant)) { |
| 344 | Alignment = Actions.ActOnNumericConstant(Info->Alignment); |
| 345 | if (Alignment.isInvalid()) |
| 346 | return; |
| 347 | } |
| 348 | Actions.ActOnPragmaPack(Info->Kind, Info->Name, Alignment.get(), PragmaLoc, |
Eli Friedman | aa5ab26 | 2012-02-23 23:47:16 +0000 | [diff] [blame] | 349 | Info->LParenLoc, Info->RParenLoc); |
Eli Friedman | aa5ab26 | 2012-02-23 23:47:16 +0000 | [diff] [blame] | 350 | } |
| 351 | |
Eli Friedman | 9595c7e | 2012-10-04 02:36:51 +0000 | [diff] [blame] | 352 | void Parser::HandlePragmaMSStruct() { |
| 353 | assert(Tok.is(tok::annot_pragma_msstruct)); |
| 354 | Sema::PragmaMSStructKind Kind = |
| 355 | static_cast<Sema::PragmaMSStructKind>( |
| 356 | reinterpret_cast<uintptr_t>(Tok.getAnnotationValue())); |
| 357 | Actions.ActOnPragmaMSStruct(Kind); |
| 358 | ConsumeToken(); // The annotation token. |
| 359 | } |
| 360 | |
| 361 | void Parser::HandlePragmaAlign() { |
| 362 | assert(Tok.is(tok::annot_pragma_align)); |
| 363 | Sema::PragmaOptionsAlignKind Kind = |
| 364 | static_cast<Sema::PragmaOptionsAlignKind>( |
| 365 | reinterpret_cast<uintptr_t>(Tok.getAnnotationValue())); |
| 366 | SourceLocation PragmaLoc = ConsumeToken(); |
| 367 | Actions.ActOnPragmaOptionsAlign(Kind, PragmaLoc); |
| 368 | } |
| 369 | |
| 370 | void Parser::HandlePragmaWeak() { |
| 371 | assert(Tok.is(tok::annot_pragma_weak)); |
| 372 | SourceLocation PragmaLoc = ConsumeToken(); |
| 373 | Actions.ActOnPragmaWeakID(Tok.getIdentifierInfo(), PragmaLoc, |
| 374 | Tok.getLocation()); |
| 375 | ConsumeToken(); // The weak name. |
| 376 | } |
| 377 | |
| 378 | void Parser::HandlePragmaWeakAlias() { |
| 379 | assert(Tok.is(tok::annot_pragma_weakalias)); |
| 380 | SourceLocation PragmaLoc = ConsumeToken(); |
| 381 | IdentifierInfo *WeakName = Tok.getIdentifierInfo(); |
| 382 | SourceLocation WeakNameLoc = Tok.getLocation(); |
| 383 | ConsumeToken(); |
| 384 | IdentifierInfo *AliasName = Tok.getIdentifierInfo(); |
| 385 | SourceLocation AliasNameLoc = Tok.getLocation(); |
| 386 | ConsumeToken(); |
| 387 | Actions.ActOnPragmaWeakAlias(WeakName, AliasName, PragmaLoc, |
| 388 | WeakNameLoc, AliasNameLoc); |
| 389 | |
| 390 | } |
| 391 | |
| 392 | void Parser::HandlePragmaRedefineExtname() { |
| 393 | assert(Tok.is(tok::annot_pragma_redefine_extname)); |
| 394 | SourceLocation RedefLoc = ConsumeToken(); |
| 395 | IdentifierInfo *RedefName = Tok.getIdentifierInfo(); |
| 396 | SourceLocation RedefNameLoc = Tok.getLocation(); |
| 397 | ConsumeToken(); |
| 398 | IdentifierInfo *AliasName = Tok.getIdentifierInfo(); |
| 399 | SourceLocation AliasNameLoc = Tok.getLocation(); |
| 400 | ConsumeToken(); |
| 401 | Actions.ActOnPragmaRedefineExtname(RedefName, AliasName, RedefLoc, |
| 402 | RedefNameLoc, AliasNameLoc); |
| 403 | } |
| 404 | |
| 405 | void Parser::HandlePragmaFPContract() { |
| 406 | assert(Tok.is(tok::annot_pragma_fp_contract)); |
| 407 | tok::OnOffSwitch OOS = |
| 408 | static_cast<tok::OnOffSwitch>( |
| 409 | reinterpret_cast<uintptr_t>(Tok.getAnnotationValue())); |
| 410 | Actions.ActOnPragmaFPContract(OOS); |
| 411 | ConsumeToken(); // The annotation token. |
| 412 | } |
| 413 | |
Tareq A. Siraj | 85192c7 | 2013-04-16 18:41:26 +0000 | [diff] [blame] | 414 | StmtResult Parser::HandlePragmaCaptured() |
| 415 | { |
| 416 | assert(Tok.is(tok::annot_pragma_captured)); |
| 417 | ConsumeToken(); |
| 418 | |
| 419 | if (Tok.isNot(tok::l_brace)) { |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 420 | PP.Diag(Tok, diag::err_expected) << tok::l_brace; |
Tareq A. Siraj | 85192c7 | 2013-04-16 18:41:26 +0000 | [diff] [blame] | 421 | return StmtError(); |
| 422 | } |
| 423 | |
Tareq A. Siraj | 6afcf88 | 2013-04-16 19:37:38 +0000 | [diff] [blame] | 424 | SourceLocation Loc = Tok.getLocation(); |
| 425 | |
| 426 | ParseScope CapturedRegionScope(this, Scope::FnScope | Scope::DeclScope); |
Ben Langmuir | 8c045ac | 2013-05-03 19:00:33 +0000 | [diff] [blame] | 427 | Actions.ActOnCapturedRegionStart(Loc, getCurScope(), CR_Default, |
| 428 | /*NumParams=*/1); |
Tareq A. Siraj | 6afcf88 | 2013-04-16 19:37:38 +0000 | [diff] [blame] | 429 | |
| 430 | StmtResult R = ParseCompoundStatement(); |
| 431 | CapturedRegionScope.Exit(); |
| 432 | |
| 433 | if (R.isInvalid()) { |
| 434 | Actions.ActOnCapturedRegionError(); |
| 435 | return StmtError(); |
| 436 | } |
| 437 | |
| 438 | return Actions.ActOnCapturedRegionEnd(R.get()); |
Tareq A. Siraj | 85192c7 | 2013-04-16 18:41:26 +0000 | [diff] [blame] | 439 | } |
| 440 | |
Eli Friedman | 9595c7e | 2012-10-04 02:36:51 +0000 | [diff] [blame] | 441 | namespace { |
| 442 | typedef llvm::PointerIntPair<IdentifierInfo *, 1, bool> OpenCLExtData; |
| 443 | } |
| 444 | |
| 445 | void Parser::HandlePragmaOpenCLExtension() { |
| 446 | assert(Tok.is(tok::annot_pragma_opencl_extension)); |
| 447 | OpenCLExtData data = |
| 448 | OpenCLExtData::getFromOpaqueValue(Tok.getAnnotationValue()); |
| 449 | unsigned state = data.getInt(); |
| 450 | IdentifierInfo *ename = data.getPointer(); |
| 451 | SourceLocation NameLoc = Tok.getLocation(); |
| 452 | ConsumeToken(); // The annotation token. |
| 453 | |
| 454 | OpenCLOptions &f = Actions.getOpenCLOptions(); |
| 455 | // OpenCL 1.1 9.1: "The all variant sets the behavior for all extensions, |
| 456 | // overriding all previously issued extension directives, but only if the |
| 457 | // behavior is set to disable." |
| 458 | if (state == 0 && ename->isStr("all")) { |
| 459 | #define OPENCLEXT(nm) f.nm = 0; |
| 460 | #include "clang/Basic/OpenCLExtensions.def" |
| 461 | } |
| 462 | #define OPENCLEXT(nm) else if (ename->isStr(#nm)) { f.nm = state; } |
| 463 | #include "clang/Basic/OpenCLExtensions.def" |
| 464 | else { |
| 465 | PP.Diag(NameLoc, diag::warn_pragma_unknown_extension) << ename; |
| 466 | return; |
| 467 | } |
| 468 | } |
| 469 | |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 470 | void Parser::HandlePragmaMSPointersToMembers() { |
| 471 | assert(Tok.is(tok::annot_pragma_ms_pointers_to_members)); |
| 472 | LangOptions::PragmaMSPointersToMembersKind RepresentationMethod = |
| 473 | static_cast<LangOptions::PragmaMSPointersToMembersKind>( |
| 474 | reinterpret_cast<uintptr_t>(Tok.getAnnotationValue())); |
| 475 | SourceLocation PragmaLoc = ConsumeToken(); // The annotation token. |
| 476 | Actions.ActOnPragmaMSPointersToMembers(RepresentationMethod, PragmaLoc); |
| 477 | } |
Tareq A. Siraj | 85192c7 | 2013-04-16 18:41:26 +0000 | [diff] [blame] | 478 | |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 479 | void Parser::HandlePragmaMSVtorDisp() { |
| 480 | assert(Tok.is(tok::annot_pragma_ms_vtordisp)); |
| 481 | uintptr_t Value = reinterpret_cast<uintptr_t>(Tok.getAnnotationValue()); |
| 482 | Sema::PragmaVtorDispKind Kind = |
| 483 | static_cast<Sema::PragmaVtorDispKind>((Value >> 16) & 0xFFFF); |
| 484 | MSVtorDispAttr::Mode Mode = MSVtorDispAttr::Mode(Value & 0xFFFF); |
| 485 | SourceLocation PragmaLoc = ConsumeToken(); // The annotation token. |
| 486 | Actions.ActOnPragmaMSVtorDisp(Kind, PragmaLoc, Mode); |
| 487 | } |
Tareq A. Siraj | 85192c7 | 2013-04-16 18:41:26 +0000 | [diff] [blame] | 488 | |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame] | 489 | void Parser::HandlePragmaMSPragma() { |
| 490 | assert(Tok.is(tok::annot_pragma_ms_pragma)); |
| 491 | // Grab the tokens out of the annotation and enter them into the stream. |
| 492 | auto TheTokens = (std::pair<Token*, size_t> *)Tok.getAnnotationValue(); |
| 493 | PP.EnterTokenStream(TheTokens->first, TheTokens->second, true, true); |
| 494 | SourceLocation PragmaLocation = ConsumeToken(); // The annotation token. |
| 495 | assert(Tok.isAnyIdentifier()); |
Stephen Hines | 176edba | 2014-12-01 14:53:08 -0800 | [diff] [blame] | 496 | StringRef PragmaName = Tok.getIdentifierInfo()->getName(); |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame] | 497 | PP.Lex(Tok); // pragma kind |
Stephen Hines | 176edba | 2014-12-01 14:53:08 -0800 | [diff] [blame] | 498 | |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame] | 499 | // Figure out which #pragma we're dealing with. The switch has no default |
| 500 | // because lex shouldn't emit the annotation token for unrecognized pragmas. |
Stephen Hines | 176edba | 2014-12-01 14:53:08 -0800 | [diff] [blame] | 501 | typedef bool (Parser::*PragmaHandler)(StringRef, SourceLocation); |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame] | 502 | PragmaHandler Handler = llvm::StringSwitch<PragmaHandler>(PragmaName) |
| 503 | .Case("data_seg", &Parser::HandlePragmaMSSegment) |
| 504 | .Case("bss_seg", &Parser::HandlePragmaMSSegment) |
| 505 | .Case("const_seg", &Parser::HandlePragmaMSSegment) |
| 506 | .Case("code_seg", &Parser::HandlePragmaMSSegment) |
| 507 | .Case("section", &Parser::HandlePragmaMSSection) |
| 508 | .Case("init_seg", &Parser::HandlePragmaMSInitSeg); |
Stephen Hines | 176edba | 2014-12-01 14:53:08 -0800 | [diff] [blame] | 509 | |
| 510 | if (!(this->*Handler)(PragmaName, PragmaLocation)) { |
| 511 | // Pragma handling failed, and has been diagnosed. Slurp up the tokens |
| 512 | // until eof (really end of line) to prevent follow-on errors. |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame] | 513 | while (Tok.isNot(tok::eof)) |
| 514 | PP.Lex(Tok); |
| 515 | PP.Lex(Tok); |
| 516 | } |
| 517 | } |
| 518 | |
Stephen Hines | 176edba | 2014-12-01 14:53:08 -0800 | [diff] [blame] | 519 | bool Parser::HandlePragmaMSSection(StringRef PragmaName, |
| 520 | SourceLocation PragmaLocation) { |
| 521 | if (Tok.isNot(tok::l_paren)) { |
| 522 | PP.Diag(PragmaLocation, diag::warn_pragma_expected_lparen) << PragmaName; |
| 523 | return false; |
| 524 | } |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame] | 525 | PP.Lex(Tok); // ( |
| 526 | // Parsing code for pragma section |
Stephen Hines | 176edba | 2014-12-01 14:53:08 -0800 | [diff] [blame] | 527 | if (Tok.isNot(tok::string_literal)) { |
| 528 | PP.Diag(PragmaLocation, diag::warn_pragma_expected_section_name) |
| 529 | << PragmaName; |
| 530 | return false; |
| 531 | } |
| 532 | ExprResult StringResult = ParseStringLiteralExpression(); |
| 533 | if (StringResult.isInvalid()) |
| 534 | return false; // Already diagnosed. |
| 535 | StringLiteral *SegmentName = cast<StringLiteral>(StringResult.get()); |
| 536 | if (SegmentName->getCharByteWidth() != 1) { |
| 537 | PP.Diag(PragmaLocation, diag::warn_pragma_expected_non_wide_string) |
| 538 | << PragmaName; |
| 539 | return false; |
| 540 | } |
| 541 | int SectionFlags = ASTContext::PSF_Read; |
| 542 | bool SectionFlagsAreDefault = true; |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame] | 543 | while (Tok.is(tok::comma)) { |
| 544 | PP.Lex(Tok); // , |
Stephen Hines | 176edba | 2014-12-01 14:53:08 -0800 | [diff] [blame] | 545 | // Ignore "long" and "short". |
| 546 | // They are undocumented, but widely used, section attributes which appear |
| 547 | // to do nothing. |
| 548 | if (Tok.is(tok::kw_long) || Tok.is(tok::kw_short)) { |
| 549 | PP.Lex(Tok); // long/short |
| 550 | continue; |
| 551 | } |
| 552 | |
| 553 | if (!Tok.isAnyIdentifier()) { |
| 554 | PP.Diag(PragmaLocation, diag::warn_pragma_expected_action_or_r_paren) |
| 555 | << PragmaName; |
| 556 | return false; |
| 557 | } |
| 558 | ASTContext::PragmaSectionFlag Flag = |
| 559 | llvm::StringSwitch<ASTContext::PragmaSectionFlag>( |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame] | 560 | Tok.getIdentifierInfo()->getName()) |
Stephen Hines | 176edba | 2014-12-01 14:53:08 -0800 | [diff] [blame] | 561 | .Case("read", ASTContext::PSF_Read) |
| 562 | .Case("write", ASTContext::PSF_Write) |
| 563 | .Case("execute", ASTContext::PSF_Execute) |
| 564 | .Case("shared", ASTContext::PSF_Invalid) |
| 565 | .Case("nopage", ASTContext::PSF_Invalid) |
| 566 | .Case("nocache", ASTContext::PSF_Invalid) |
| 567 | .Case("discard", ASTContext::PSF_Invalid) |
| 568 | .Case("remove", ASTContext::PSF_Invalid) |
| 569 | .Default(ASTContext::PSF_None); |
| 570 | if (Flag == ASTContext::PSF_None || Flag == ASTContext::PSF_Invalid) { |
| 571 | PP.Diag(PragmaLocation, Flag == ASTContext::PSF_None |
| 572 | ? diag::warn_pragma_invalid_specific_action |
| 573 | : diag::warn_pragma_unsupported_action) |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame] | 574 | << PragmaName << Tok.getIdentifierInfo()->getName(); |
Stephen Hines | 176edba | 2014-12-01 14:53:08 -0800 | [diff] [blame] | 575 | return false; |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame] | 576 | } |
| 577 | SectionFlags |= Flag; |
Stephen Hines | 176edba | 2014-12-01 14:53:08 -0800 | [diff] [blame] | 578 | SectionFlagsAreDefault = false; |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame] | 579 | PP.Lex(Tok); // Identifier |
| 580 | } |
Stephen Hines | 176edba | 2014-12-01 14:53:08 -0800 | [diff] [blame] | 581 | // If no section attributes are specified, the section will be marked as |
| 582 | // read/write. |
| 583 | if (SectionFlagsAreDefault) |
| 584 | SectionFlags |= ASTContext::PSF_Write; |
| 585 | if (Tok.isNot(tok::r_paren)) { |
| 586 | PP.Diag(PragmaLocation, diag::warn_pragma_expected_rparen) << PragmaName; |
| 587 | return false; |
| 588 | } |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame] | 589 | PP.Lex(Tok); // ) |
Stephen Hines | 176edba | 2014-12-01 14:53:08 -0800 | [diff] [blame] | 590 | if (Tok.isNot(tok::eof)) { |
| 591 | PP.Diag(PragmaLocation, diag::warn_pragma_extra_tokens_at_eol) |
| 592 | << PragmaName; |
| 593 | return false; |
| 594 | } |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame] | 595 | PP.Lex(Tok); // eof |
| 596 | Actions.ActOnPragmaMSSection(PragmaLocation, SectionFlags, SegmentName); |
Stephen Hines | 176edba | 2014-12-01 14:53:08 -0800 | [diff] [blame] | 597 | return true; |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame] | 598 | } |
| 599 | |
Stephen Hines | 176edba | 2014-12-01 14:53:08 -0800 | [diff] [blame] | 600 | bool Parser::HandlePragmaMSSegment(StringRef PragmaName, |
| 601 | SourceLocation PragmaLocation) { |
| 602 | if (Tok.isNot(tok::l_paren)) { |
| 603 | PP.Diag(PragmaLocation, diag::warn_pragma_expected_lparen) << PragmaName; |
| 604 | return false; |
| 605 | } |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame] | 606 | PP.Lex(Tok); // ( |
| 607 | Sema::PragmaMsStackAction Action = Sema::PSK_Reset; |
Stephen Hines | 176edba | 2014-12-01 14:53:08 -0800 | [diff] [blame] | 608 | StringRef SlotLabel; |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame] | 609 | if (Tok.isAnyIdentifier()) { |
Stephen Hines | 176edba | 2014-12-01 14:53:08 -0800 | [diff] [blame] | 610 | StringRef PushPop = Tok.getIdentifierInfo()->getName(); |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame] | 611 | if (PushPop == "push") |
| 612 | Action = Sema::PSK_Push; |
| 613 | else if (PushPop == "pop") |
| 614 | Action = Sema::PSK_Pop; |
Stephen Hines | 176edba | 2014-12-01 14:53:08 -0800 | [diff] [blame] | 615 | else { |
| 616 | PP.Diag(PragmaLocation, |
| 617 | diag::warn_pragma_expected_section_push_pop_or_name) |
| 618 | << PragmaName; |
| 619 | return false; |
| 620 | } |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame] | 621 | if (Action != Sema::PSK_Reset) { |
| 622 | PP.Lex(Tok); // push | pop |
| 623 | if (Tok.is(tok::comma)) { |
| 624 | PP.Lex(Tok); // , |
| 625 | // If we've got a comma, we either need a label or a string. |
| 626 | if (Tok.isAnyIdentifier()) { |
| 627 | SlotLabel = Tok.getIdentifierInfo()->getName(); |
| 628 | PP.Lex(Tok); // identifier |
| 629 | if (Tok.is(tok::comma)) |
| 630 | PP.Lex(Tok); |
Stephen Hines | 176edba | 2014-12-01 14:53:08 -0800 | [diff] [blame] | 631 | else if (Tok.isNot(tok::r_paren)) { |
| 632 | PP.Diag(PragmaLocation, diag::warn_pragma_expected_punc) |
| 633 | << PragmaName; |
| 634 | return false; |
| 635 | } |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame] | 636 | } |
Stephen Hines | 176edba | 2014-12-01 14:53:08 -0800 | [diff] [blame] | 637 | } else if (Tok.isNot(tok::r_paren)) { |
| 638 | PP.Diag(PragmaLocation, diag::warn_pragma_expected_punc) << PragmaName; |
| 639 | return false; |
| 640 | } |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame] | 641 | } |
| 642 | } |
| 643 | // Grab the string literal for our section name. |
| 644 | StringLiteral *SegmentName = nullptr; |
| 645 | if (Tok.isNot(tok::r_paren)) { |
Stephen Hines | 176edba | 2014-12-01 14:53:08 -0800 | [diff] [blame] | 646 | if (Tok.isNot(tok::string_literal)) { |
| 647 | unsigned DiagID = Action != Sema::PSK_Reset ? !SlotLabel.empty() ? |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame] | 648 | diag::warn_pragma_expected_section_name : |
| 649 | diag::warn_pragma_expected_section_label_or_name : |
| 650 | diag::warn_pragma_expected_section_push_pop_or_name; |
Stephen Hines | 176edba | 2014-12-01 14:53:08 -0800 | [diff] [blame] | 651 | PP.Diag(PragmaLocation, DiagID) << PragmaName; |
| 652 | return false; |
| 653 | } |
| 654 | ExprResult StringResult = ParseStringLiteralExpression(); |
| 655 | if (StringResult.isInvalid()) |
| 656 | return false; // Already diagnosed. |
| 657 | SegmentName = cast<StringLiteral>(StringResult.get()); |
| 658 | if (SegmentName->getCharByteWidth() != 1) { |
| 659 | PP.Diag(PragmaLocation, diag::warn_pragma_expected_non_wide_string) |
| 660 | << PragmaName; |
| 661 | return false; |
| 662 | } |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame] | 663 | // Setting section "" has no effect |
| 664 | if (SegmentName->getLength()) |
| 665 | Action = (Sema::PragmaMsStackAction)(Action | Sema::PSK_Set); |
| 666 | } |
Stephen Hines | 176edba | 2014-12-01 14:53:08 -0800 | [diff] [blame] | 667 | if (Tok.isNot(tok::r_paren)) { |
| 668 | PP.Diag(PragmaLocation, diag::warn_pragma_expected_rparen) << PragmaName; |
| 669 | return false; |
| 670 | } |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame] | 671 | PP.Lex(Tok); // ) |
Stephen Hines | 176edba | 2014-12-01 14:53:08 -0800 | [diff] [blame] | 672 | if (Tok.isNot(tok::eof)) { |
| 673 | PP.Diag(PragmaLocation, diag::warn_pragma_extra_tokens_at_eol) |
| 674 | << PragmaName; |
| 675 | return false; |
| 676 | } |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame] | 677 | PP.Lex(Tok); // eof |
| 678 | Actions.ActOnPragmaMSSeg(PragmaLocation, Action, SlotLabel, |
| 679 | SegmentName, PragmaName); |
Stephen Hines | 176edba | 2014-12-01 14:53:08 -0800 | [diff] [blame] | 680 | return true; |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame] | 681 | } |
| 682 | |
Stephen Hines | 176edba | 2014-12-01 14:53:08 -0800 | [diff] [blame] | 683 | // #pragma init_seg({ compiler | lib | user | "section-name" [, func-name]} ) |
| 684 | bool Parser::HandlePragmaMSInitSeg(StringRef PragmaName, |
| 685 | SourceLocation PragmaLocation) { |
| 686 | if (getTargetInfo().getTriple().getEnvironment() != llvm::Triple::MSVC) { |
| 687 | PP.Diag(PragmaLocation, diag::warn_pragma_init_seg_unsupported_target); |
| 688 | return false; |
| 689 | } |
| 690 | |
| 691 | if (ExpectAndConsume(tok::l_paren, diag::warn_pragma_expected_lparen, |
| 692 | PragmaName)) |
| 693 | return false; |
| 694 | |
| 695 | // Parse either the known section names or the string section name. |
| 696 | StringLiteral *SegmentName = nullptr; |
| 697 | if (Tok.isAnyIdentifier()) { |
| 698 | auto *II = Tok.getIdentifierInfo(); |
| 699 | StringRef Section = llvm::StringSwitch<StringRef>(II->getName()) |
| 700 | .Case("compiler", "\".CRT$XCC\"") |
| 701 | .Case("lib", "\".CRT$XCL\"") |
| 702 | .Case("user", "\".CRT$XCU\"") |
| 703 | .Default(""); |
| 704 | |
| 705 | if (!Section.empty()) { |
| 706 | // Pretend the user wrote the appropriate string literal here. |
| 707 | Token Toks[1]; |
| 708 | Toks[0].startToken(); |
| 709 | Toks[0].setKind(tok::string_literal); |
| 710 | Toks[0].setLocation(Tok.getLocation()); |
| 711 | Toks[0].setLiteralData(Section.data()); |
| 712 | Toks[0].setLength(Section.size()); |
| 713 | SegmentName = |
| 714 | cast<StringLiteral>(Actions.ActOnStringLiteral(Toks, nullptr).get()); |
| 715 | PP.Lex(Tok); |
| 716 | } |
| 717 | } else if (Tok.is(tok::string_literal)) { |
| 718 | ExprResult StringResult = ParseStringLiteralExpression(); |
| 719 | if (StringResult.isInvalid()) |
| 720 | return false; |
| 721 | SegmentName = cast<StringLiteral>(StringResult.get()); |
| 722 | if (SegmentName->getCharByteWidth() != 1) { |
| 723 | PP.Diag(PragmaLocation, diag::warn_pragma_expected_non_wide_string) |
| 724 | << PragmaName; |
| 725 | return false; |
| 726 | } |
| 727 | // FIXME: Add support for the '[, func-name]' part of the pragma. |
| 728 | } |
| 729 | |
| 730 | if (!SegmentName) { |
| 731 | PP.Diag(PragmaLocation, diag::warn_pragma_expected_init_seg) << PragmaName; |
| 732 | return false; |
| 733 | } |
| 734 | |
| 735 | if (ExpectAndConsume(tok::r_paren, diag::warn_pragma_expected_rparen, |
| 736 | PragmaName) || |
| 737 | ExpectAndConsume(tok::eof, diag::warn_pragma_extra_tokens_at_eol, |
| 738 | PragmaName)) |
| 739 | return false; |
| 740 | |
| 741 | Actions.ActOnPragmaMSInitSeg(PragmaLocation, SegmentName); |
| 742 | return true; |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame] | 743 | } |
| 744 | |
Stephen Hines | c568f1e | 2014-07-21 00:47:37 -0700 | [diff] [blame] | 745 | struct PragmaLoopHintInfo { |
Stephen Hines | 176edba | 2014-12-01 14:53:08 -0800 | [diff] [blame] | 746 | Token PragmaName; |
Stephen Hines | c568f1e | 2014-07-21 00:47:37 -0700 | [diff] [blame] | 747 | Token Option; |
Stephen Hines | 176edba | 2014-12-01 14:53:08 -0800 | [diff] [blame] | 748 | Token *Toks; |
| 749 | size_t TokSize; |
| 750 | PragmaLoopHintInfo() : Toks(nullptr), TokSize(0) {} |
Stephen Hines | c568f1e | 2014-07-21 00:47:37 -0700 | [diff] [blame] | 751 | }; |
| 752 | |
Stephen Hines | 176edba | 2014-12-01 14:53:08 -0800 | [diff] [blame] | 753 | static std::string PragmaLoopHintString(Token PragmaName, Token Option) { |
| 754 | std::string PragmaString; |
| 755 | if (PragmaName.getIdentifierInfo()->getName() == "loop") { |
| 756 | PragmaString = "clang loop "; |
| 757 | PragmaString += Option.getIdentifierInfo()->getName(); |
| 758 | } else { |
| 759 | assert(PragmaName.getIdentifierInfo()->getName() == "unroll" && |
| 760 | "Unexpected pragma name"); |
| 761 | PragmaString = "unroll"; |
| 762 | } |
| 763 | return PragmaString; |
| 764 | } |
| 765 | |
| 766 | bool Parser::HandlePragmaLoopHint(LoopHint &Hint) { |
Stephen Hines | c568f1e | 2014-07-21 00:47:37 -0700 | [diff] [blame] | 767 | assert(Tok.is(tok::annot_pragma_loop_hint)); |
| 768 | PragmaLoopHintInfo *Info = |
| 769 | static_cast<PragmaLoopHintInfo *>(Tok.getAnnotationValue()); |
| 770 | |
Stephen Hines | 176edba | 2014-12-01 14:53:08 -0800 | [diff] [blame] | 771 | IdentifierInfo *PragmaNameInfo = Info->PragmaName.getIdentifierInfo(); |
| 772 | Hint.PragmaNameLoc = IdentifierLoc::create( |
| 773 | Actions.Context, Info->PragmaName.getLocation(), PragmaNameInfo); |
Stephen Hines | c568f1e | 2014-07-21 00:47:37 -0700 | [diff] [blame] | 774 | |
Stephen Hines | 176edba | 2014-12-01 14:53:08 -0800 | [diff] [blame] | 775 | // It is possible that the loop hint has no option identifier, such as |
| 776 | // #pragma unroll(4). |
| 777 | IdentifierInfo *OptionInfo = Info->Option.is(tok::identifier) |
| 778 | ? Info->Option.getIdentifierInfo() |
| 779 | : nullptr; |
| 780 | Hint.OptionLoc = IdentifierLoc::create( |
| 781 | Actions.Context, Info->Option.getLocation(), OptionInfo); |
Stephen Hines | c568f1e | 2014-07-21 00:47:37 -0700 | [diff] [blame] | 782 | |
Stephen Hines | 176edba | 2014-12-01 14:53:08 -0800 | [diff] [blame] | 783 | Token *Toks = Info->Toks; |
| 784 | size_t TokSize = Info->TokSize; |
| 785 | |
| 786 | // Return a valid hint if pragma unroll or nounroll were specified |
| 787 | // without an argument. |
| 788 | bool PragmaUnroll = PragmaNameInfo->getName() == "unroll"; |
| 789 | bool PragmaNoUnroll = PragmaNameInfo->getName() == "nounroll"; |
| 790 | if (TokSize == 0 && (PragmaUnroll || PragmaNoUnroll)) { |
| 791 | ConsumeToken(); // The annotation token. |
| 792 | Hint.Range = Info->PragmaName.getLocation(); |
| 793 | return true; |
| 794 | } |
| 795 | |
| 796 | // The constant expression is always followed by an eof token, which increases |
| 797 | // the TokSize by 1. |
| 798 | assert(TokSize > 0 && |
| 799 | "PragmaLoopHintInfo::Toks must contain at least one token."); |
| 800 | |
| 801 | // If no option is specified the argument is assumed to be a constant expr. |
| 802 | bool StateOption = false; |
| 803 | if (OptionInfo) { // Pragma unroll does not specify an option. |
| 804 | StateOption = llvm::StringSwitch<bool>(OptionInfo->getName()) |
| 805 | .Case("vectorize", true) |
| 806 | .Case("interleave", true) |
| 807 | .Case("unroll", true) |
| 808 | .Default(false); |
| 809 | } |
| 810 | |
| 811 | // Verify loop hint has an argument. |
| 812 | if (Toks[0].is(tok::eof)) { |
| 813 | ConsumeToken(); // The annotation token. |
| 814 | Diag(Toks[0].getLocation(), diag::err_pragma_loop_missing_argument) |
| 815 | << /*StateArgument=*/StateOption << /*FullKeyword=*/PragmaUnroll; |
| 816 | return false; |
| 817 | } |
| 818 | |
| 819 | // Validate the argument. |
| 820 | if (StateOption) { |
| 821 | ConsumeToken(); // The annotation token. |
| 822 | bool OptionUnroll = OptionInfo->isStr("unroll"); |
| 823 | SourceLocation StateLoc = Toks[0].getLocation(); |
| 824 | IdentifierInfo *StateInfo = Toks[0].getIdentifierInfo(); |
| 825 | if (!StateInfo || ((OptionUnroll ? !StateInfo->isStr("full") |
| 826 | : !StateInfo->isStr("enable")) && |
| 827 | !StateInfo->isStr("disable"))) { |
| 828 | Diag(Toks[0].getLocation(), diag::err_pragma_invalid_keyword) |
| 829 | << /*FullKeyword=*/OptionUnroll; |
| 830 | return false; |
| 831 | } |
| 832 | if (TokSize > 2) |
| 833 | Diag(Tok.getLocation(), diag::warn_pragma_extra_tokens_at_eol) |
| 834 | << PragmaLoopHintString(Info->PragmaName, Info->Option); |
| 835 | Hint.StateLoc = IdentifierLoc::create(Actions.Context, StateLoc, StateInfo); |
| 836 | } else { |
| 837 | // Enter constant expression including eof terminator into token stream. |
| 838 | PP.EnterTokenStream(Toks, TokSize, /*DisableMacroExpansion=*/false, |
| 839 | /*OwnsTokens=*/false); |
| 840 | ConsumeToken(); // The annotation token. |
| 841 | |
| 842 | ExprResult R = ParseConstantExpression(); |
| 843 | |
| 844 | // Tokens following an error in an ill-formed constant expression will |
| 845 | // remain in the token stream and must be removed. |
| 846 | if (Tok.isNot(tok::eof)) { |
| 847 | Diag(Tok.getLocation(), diag::warn_pragma_extra_tokens_at_eol) |
| 848 | << PragmaLoopHintString(Info->PragmaName, Info->Option); |
| 849 | while (Tok.isNot(tok::eof)) |
| 850 | ConsumeAnyToken(); |
| 851 | } |
| 852 | |
| 853 | ConsumeToken(); // Consume the constant expression eof terminator. |
| 854 | |
| 855 | if (R.isInvalid() || |
| 856 | Actions.CheckLoopHintExpr(R.get(), Toks[0].getLocation())) |
| 857 | return false; |
| 858 | |
| 859 | // Argument is a constant expression with an integer type. |
| 860 | Hint.ValueExpr = R.get(); |
| 861 | } |
| 862 | |
| 863 | Hint.Range = SourceRange(Info->PragmaName.getLocation(), |
| 864 | Info->Toks[TokSize - 1].getLocation()); |
| 865 | return true; |
Stephen Hines | c568f1e | 2014-07-21 00:47:37 -0700 | [diff] [blame] | 866 | } |
| 867 | |
Eli Friedman | aa8b0d1 | 2010-08-05 06:57:20 +0000 | [diff] [blame] | 868 | // #pragma GCC visibility comes in two variants: |
| 869 | // 'push' '(' [visibility] ')' |
| 870 | // 'pop' |
Douglas Gregor | 80c60f7 | 2010-09-09 22:45:38 +0000 | [diff] [blame] | 871 | void PragmaGCCVisibilityHandler::HandlePragma(Preprocessor &PP, |
| 872 | PragmaIntroducerKind Introducer, |
| 873 | Token &VisTok) { |
Eli Friedman | aa8b0d1 | 2010-08-05 06:57:20 +0000 | [diff] [blame] | 874 | SourceLocation VisLoc = VisTok.getLocation(); |
| 875 | |
| 876 | Token Tok; |
Joerg Sonnenberger | e23af2a | 2011-07-20 01:03:50 +0000 | [diff] [blame] | 877 | PP.LexUnexpandedToken(Tok); |
Eli Friedman | aa8b0d1 | 2010-08-05 06:57:20 +0000 | [diff] [blame] | 878 | |
| 879 | const IdentifierInfo *PushPop = Tok.getIdentifierInfo(); |
| 880 | |
Eli Friedman | aa8b0d1 | 2010-08-05 06:57:20 +0000 | [diff] [blame] | 881 | const IdentifierInfo *VisType; |
| 882 | if (PushPop && PushPop->isStr("pop")) { |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame] | 883 | VisType = nullptr; |
Eli Friedman | aa8b0d1 | 2010-08-05 06:57:20 +0000 | [diff] [blame] | 884 | } else if (PushPop && PushPop->isStr("push")) { |
Joerg Sonnenberger | e23af2a | 2011-07-20 01:03:50 +0000 | [diff] [blame] | 885 | PP.LexUnexpandedToken(Tok); |
Eli Friedman | aa8b0d1 | 2010-08-05 06:57:20 +0000 | [diff] [blame] | 886 | if (Tok.isNot(tok::l_paren)) { |
| 887 | PP.Diag(Tok.getLocation(), diag::warn_pragma_expected_lparen) |
| 888 | << "visibility"; |
| 889 | return; |
| 890 | } |
Joerg Sonnenberger | e23af2a | 2011-07-20 01:03:50 +0000 | [diff] [blame] | 891 | PP.LexUnexpandedToken(Tok); |
Eli Friedman | aa8b0d1 | 2010-08-05 06:57:20 +0000 | [diff] [blame] | 892 | VisType = Tok.getIdentifierInfo(); |
| 893 | if (!VisType) { |
| 894 | PP.Diag(Tok.getLocation(), diag::warn_pragma_expected_identifier) |
| 895 | << "visibility"; |
| 896 | return; |
| 897 | } |
Joerg Sonnenberger | e23af2a | 2011-07-20 01:03:50 +0000 | [diff] [blame] | 898 | PP.LexUnexpandedToken(Tok); |
Eli Friedman | aa8b0d1 | 2010-08-05 06:57:20 +0000 | [diff] [blame] | 899 | if (Tok.isNot(tok::r_paren)) { |
| 900 | PP.Diag(Tok.getLocation(), diag::warn_pragma_expected_rparen) |
| 901 | << "visibility"; |
| 902 | return; |
| 903 | } |
| 904 | } else { |
| 905 | PP.Diag(Tok.getLocation(), diag::warn_pragma_expected_identifier) |
| 906 | << "visibility"; |
| 907 | return; |
| 908 | } |
Joerg Sonnenberger | e23af2a | 2011-07-20 01:03:50 +0000 | [diff] [blame] | 909 | PP.LexUnexpandedToken(Tok); |
Peter Collingbourne | 8402155 | 2011-02-28 02:37:51 +0000 | [diff] [blame] | 910 | if (Tok.isNot(tok::eod)) { |
Eli Friedman | aa8b0d1 | 2010-08-05 06:57:20 +0000 | [diff] [blame] | 911 | PP.Diag(Tok.getLocation(), diag::warn_pragma_extra_tokens_at_eol) |
| 912 | << "visibility"; |
| 913 | return; |
| 914 | } |
| 915 | |
Rafael Espindola | 426fc94 | 2012-01-26 02:02:57 +0000 | [diff] [blame] | 916 | Token *Toks = new Token[1]; |
| 917 | Toks[0].startToken(); |
| 918 | Toks[0].setKind(tok::annot_pragma_vis); |
| 919 | Toks[0].setLocation(VisLoc); |
| 920 | Toks[0].setAnnotationValue( |
| 921 | const_cast<void*>(static_cast<const void*>(VisType))); |
| 922 | PP.EnterTokenStream(Toks, 1, /*DisableMacroExpansion=*/true, |
| 923 | /*OwnsTokens=*/true); |
Eli Friedman | aa8b0d1 | 2010-08-05 06:57:20 +0000 | [diff] [blame] | 924 | } |
| 925 | |
Daniel Dunbar | fcdd8fe | 2008-10-04 19:21:03 +0000 | [diff] [blame] | 926 | // #pragma pack(...) comes in the following delicious flavors: |
| 927 | // pack '(' [integer] ')' |
| 928 | // pack '(' 'show' ')' |
| 929 | // pack '(' ('push' | 'pop') [',' identifier] [, integer] ')' |
Douglas Gregor | 80c60f7 | 2010-09-09 22:45:38 +0000 | [diff] [blame] | 930 | void PragmaPackHandler::HandlePragma(Preprocessor &PP, |
| 931 | PragmaIntroducerKind Introducer, |
| 932 | Token &PackTok) { |
Daniel Dunbar | fcdd8fe | 2008-10-04 19:21:03 +0000 | [diff] [blame] | 933 | SourceLocation PackLoc = PackTok.getLocation(); |
| 934 | |
| 935 | Token Tok; |
| 936 | PP.Lex(Tok); |
| 937 | if (Tok.isNot(tok::l_paren)) { |
Ted Kremenek | 4726d03 | 2009-03-23 22:28:25 +0000 | [diff] [blame] | 938 | PP.Diag(Tok.getLocation(), diag::warn_pragma_expected_lparen) << "pack"; |
Daniel Dunbar | fcdd8fe | 2008-10-04 19:21:03 +0000 | [diff] [blame] | 939 | return; |
| 940 | } |
| 941 | |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 942 | Sema::PragmaPackKind Kind = Sema::PPK_Default; |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame] | 943 | IdentifierInfo *Name = nullptr; |
Eli Friedman | 9595c7e | 2012-10-04 02:36:51 +0000 | [diff] [blame] | 944 | Token Alignment; |
| 945 | Alignment.startToken(); |
Daniel Dunbar | fcdd8fe | 2008-10-04 19:21:03 +0000 | [diff] [blame] | 946 | SourceLocation LParenLoc = Tok.getLocation(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 947 | PP.Lex(Tok); |
Daniel Dunbar | fcdd8fe | 2008-10-04 19:21:03 +0000 | [diff] [blame] | 948 | if (Tok.is(tok::numeric_constant)) { |
Eli Friedman | 9595c7e | 2012-10-04 02:36:51 +0000 | [diff] [blame] | 949 | Alignment = Tok; |
Daniel Dunbar | fcdd8fe | 2008-10-04 19:21:03 +0000 | [diff] [blame] | 950 | |
| 951 | PP.Lex(Tok); |
Eli Friedman | 19bda3a | 2011-11-02 01:53:16 +0000 | [diff] [blame] | 952 | |
| 953 | // In MSVC/gcc, #pragma pack(4) sets the alignment without affecting |
| 954 | // the push/pop stack. |
| 955 | // 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] | 956 | if (PP.getLangOpts().ApplePragmaPack) |
Eli Friedman | 19bda3a | 2011-11-02 01:53:16 +0000 | [diff] [blame] | 957 | Kind = Sema::PPK_Push; |
Daniel Dunbar | fcdd8fe | 2008-10-04 19:21:03 +0000 | [diff] [blame] | 958 | } else if (Tok.is(tok::identifier)) { |
| 959 | const IdentifierInfo *II = Tok.getIdentifierInfo(); |
Chris Lattner | 08631c5 | 2008-11-23 21:45:46 +0000 | [diff] [blame] | 960 | if (II->isStr("show")) { |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 961 | Kind = Sema::PPK_Show; |
Daniel Dunbar | fcdd8fe | 2008-10-04 19:21:03 +0000 | [diff] [blame] | 962 | PP.Lex(Tok); |
| 963 | } else { |
Chris Lattner | 08631c5 | 2008-11-23 21:45:46 +0000 | [diff] [blame] | 964 | if (II->isStr("push")) { |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 965 | Kind = Sema::PPK_Push; |
Chris Lattner | 08631c5 | 2008-11-23 21:45:46 +0000 | [diff] [blame] | 966 | } else if (II->isStr("pop")) { |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 967 | Kind = Sema::PPK_Pop; |
Daniel Dunbar | fcdd8fe | 2008-10-04 19:21:03 +0000 | [diff] [blame] | 968 | } else { |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 969 | PP.Diag(Tok.getLocation(), diag::warn_pragma_invalid_action) << "pack"; |
Daniel Dunbar | fcdd8fe | 2008-10-04 19:21:03 +0000 | [diff] [blame] | 970 | return; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 971 | } |
Daniel Dunbar | fcdd8fe | 2008-10-04 19:21:03 +0000 | [diff] [blame] | 972 | PP.Lex(Tok); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 973 | |
Daniel Dunbar | fcdd8fe | 2008-10-04 19:21:03 +0000 | [diff] [blame] | 974 | if (Tok.is(tok::comma)) { |
| 975 | PP.Lex(Tok); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 976 | |
Daniel Dunbar | fcdd8fe | 2008-10-04 19:21:03 +0000 | [diff] [blame] | 977 | if (Tok.is(tok::numeric_constant)) { |
Eli Friedman | 9595c7e | 2012-10-04 02:36:51 +0000 | [diff] [blame] | 978 | Alignment = Tok; |
Daniel Dunbar | fcdd8fe | 2008-10-04 19:21:03 +0000 | [diff] [blame] | 979 | |
| 980 | PP.Lex(Tok); |
| 981 | } else if (Tok.is(tok::identifier)) { |
| 982 | Name = Tok.getIdentifierInfo(); |
| 983 | PP.Lex(Tok); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 984 | |
Daniel Dunbar | fcdd8fe | 2008-10-04 19:21:03 +0000 | [diff] [blame] | 985 | if (Tok.is(tok::comma)) { |
| 986 | PP.Lex(Tok); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 987 | |
Daniel Dunbar | fcdd8fe | 2008-10-04 19:21:03 +0000 | [diff] [blame] | 988 | if (Tok.isNot(tok::numeric_constant)) { |
Chris Lattner | 08631c5 | 2008-11-23 21:45:46 +0000 | [diff] [blame] | 989 | PP.Diag(Tok.getLocation(), diag::warn_pragma_pack_malformed); |
Daniel Dunbar | fcdd8fe | 2008-10-04 19:21:03 +0000 | [diff] [blame] | 990 | return; |
| 991 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 992 | |
Eli Friedman | 9595c7e | 2012-10-04 02:36:51 +0000 | [diff] [blame] | 993 | Alignment = Tok; |
Daniel Dunbar | fcdd8fe | 2008-10-04 19:21:03 +0000 | [diff] [blame] | 994 | |
| 995 | PP.Lex(Tok); |
| 996 | } |
| 997 | } else { |
Chris Lattner | 08631c5 | 2008-11-23 21:45:46 +0000 | [diff] [blame] | 998 | PP.Diag(Tok.getLocation(), diag::warn_pragma_pack_malformed); |
Daniel Dunbar | fcdd8fe | 2008-10-04 19:21:03 +0000 | [diff] [blame] | 999 | return; |
| 1000 | } |
| 1001 | } |
| 1002 | } |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 1003 | } else if (PP.getLangOpts().ApplePragmaPack) { |
Eli Friedman | 19bda3a | 2011-11-02 01:53:16 +0000 | [diff] [blame] | 1004 | // In MSVC/gcc, #pragma pack() resets the alignment without affecting |
| 1005 | // the push/pop stack. |
| 1006 | // In Apple gcc #pragma pack() is equivalent to #pragma pack(pop). |
| 1007 | Kind = Sema::PPK_Pop; |
Sebastian Redl | 0e9eabc | 2008-12-09 13:15:23 +0000 | [diff] [blame] | 1008 | } |
Daniel Dunbar | fcdd8fe | 2008-10-04 19:21:03 +0000 | [diff] [blame] | 1009 | |
| 1010 | if (Tok.isNot(tok::r_paren)) { |
Ted Kremenek | 4726d03 | 2009-03-23 22:28:25 +0000 | [diff] [blame] | 1011 | PP.Diag(Tok.getLocation(), diag::warn_pragma_expected_rparen) << "pack"; |
Daniel Dunbar | fcdd8fe | 2008-10-04 19:21:03 +0000 | [diff] [blame] | 1012 | return; |
| 1013 | } |
| 1014 | |
Daniel Dunbar | 861800c | 2010-05-26 23:29:06 +0000 | [diff] [blame] | 1015 | SourceLocation RParenLoc = Tok.getLocation(); |
Eli Friedman | 9991479 | 2009-06-05 00:49:58 +0000 | [diff] [blame] | 1016 | PP.Lex(Tok); |
Peter Collingbourne | 8402155 | 2011-02-28 02:37:51 +0000 | [diff] [blame] | 1017 | if (Tok.isNot(tok::eod)) { |
Eli Friedman | 9991479 | 2009-06-05 00:49:58 +0000 | [diff] [blame] | 1018 | PP.Diag(Tok.getLocation(), diag::warn_pragma_extra_tokens_at_eol) << "pack"; |
| 1019 | return; |
| 1020 | } |
| 1021 | |
Daniel Dunbar | b093955 | 2012-02-29 01:38:22 +0000 | [diff] [blame] | 1022 | PragmaPackInfo *Info = |
| 1023 | (PragmaPackInfo*) PP.getPreprocessorAllocator().Allocate( |
| 1024 | sizeof(PragmaPackInfo), llvm::alignOf<PragmaPackInfo>()); |
| 1025 | new (Info) PragmaPackInfo(); |
Eli Friedman | aa5ab26 | 2012-02-23 23:47:16 +0000 | [diff] [blame] | 1026 | Info->Kind = Kind; |
| 1027 | Info->Name = Name; |
Eli Friedman | 9595c7e | 2012-10-04 02:36:51 +0000 | [diff] [blame] | 1028 | Info->Alignment = Alignment; |
Eli Friedman | aa5ab26 | 2012-02-23 23:47:16 +0000 | [diff] [blame] | 1029 | Info->LParenLoc = LParenLoc; |
| 1030 | Info->RParenLoc = RParenLoc; |
| 1031 | |
Daniel Dunbar | b093955 | 2012-02-29 01:38:22 +0000 | [diff] [blame] | 1032 | Token *Toks = |
| 1033 | (Token*) PP.getPreprocessorAllocator().Allocate( |
| 1034 | sizeof(Token) * 1, llvm::alignOf<Token>()); |
| 1035 | new (Toks) Token(); |
Eli Friedman | aa5ab26 | 2012-02-23 23:47:16 +0000 | [diff] [blame] | 1036 | Toks[0].startToken(); |
| 1037 | Toks[0].setKind(tok::annot_pragma_pack); |
| 1038 | Toks[0].setLocation(PackLoc); |
| 1039 | Toks[0].setAnnotationValue(static_cast<void*>(Info)); |
| 1040 | PP.EnterTokenStream(Toks, 1, /*DisableMacroExpansion=*/true, |
Daniel Dunbar | b093955 | 2012-02-29 01:38:22 +0000 | [diff] [blame] | 1041 | /*OwnsTokens=*/false); |
Daniel Dunbar | fcdd8fe | 2008-10-04 19:21:03 +0000 | [diff] [blame] | 1042 | } |
| 1043 | |
Fariborz Jahanian | 62c9258 | 2011-04-25 18:49:15 +0000 | [diff] [blame] | 1044 | // #pragma ms_struct on |
| 1045 | // #pragma ms_struct off |
| 1046 | void PragmaMSStructHandler::HandlePragma(Preprocessor &PP, |
| 1047 | PragmaIntroducerKind Introducer, |
| 1048 | Token &MSStructTok) { |
| 1049 | Sema::PragmaMSStructKind Kind = Sema::PMSST_OFF; |
| 1050 | |
| 1051 | Token Tok; |
| 1052 | PP.Lex(Tok); |
| 1053 | if (Tok.isNot(tok::identifier)) { |
| 1054 | PP.Diag(Tok.getLocation(), diag::warn_pragma_ms_struct); |
| 1055 | return; |
| 1056 | } |
| 1057 | const IdentifierInfo *II = Tok.getIdentifierInfo(); |
| 1058 | if (II->isStr("on")) { |
| 1059 | Kind = Sema::PMSST_ON; |
| 1060 | PP.Lex(Tok); |
| 1061 | } |
| 1062 | else if (II->isStr("off") || II->isStr("reset")) |
| 1063 | PP.Lex(Tok); |
| 1064 | else { |
| 1065 | PP.Diag(Tok.getLocation(), diag::warn_pragma_ms_struct); |
| 1066 | return; |
| 1067 | } |
| 1068 | |
| 1069 | if (Tok.isNot(tok::eod)) { |
Daniel Dunbar | b093955 | 2012-02-29 01:38:22 +0000 | [diff] [blame] | 1070 | PP.Diag(Tok.getLocation(), diag::warn_pragma_extra_tokens_at_eol) |
| 1071 | << "ms_struct"; |
Fariborz Jahanian | 62c9258 | 2011-04-25 18:49:15 +0000 | [diff] [blame] | 1072 | return; |
| 1073 | } |
Eli Friedman | 9595c7e | 2012-10-04 02:36:51 +0000 | [diff] [blame] | 1074 | |
| 1075 | Token *Toks = |
| 1076 | (Token*) PP.getPreprocessorAllocator().Allocate( |
| 1077 | sizeof(Token) * 1, llvm::alignOf<Token>()); |
| 1078 | new (Toks) Token(); |
| 1079 | Toks[0].startToken(); |
| 1080 | Toks[0].setKind(tok::annot_pragma_msstruct); |
| 1081 | Toks[0].setLocation(MSStructTok.getLocation()); |
| 1082 | Toks[0].setAnnotationValue(reinterpret_cast<void*>( |
| 1083 | static_cast<uintptr_t>(Kind))); |
| 1084 | PP.EnterTokenStream(Toks, 1, /*DisableMacroExpansion=*/true, |
| 1085 | /*OwnsTokens=*/false); |
Fariborz Jahanian | 62c9258 | 2011-04-25 18:49:15 +0000 | [diff] [blame] | 1086 | } |
| 1087 | |
Daniel Dunbar | cbb98ed | 2010-07-31 19:17:07 +0000 | [diff] [blame] | 1088 | // #pragma 'align' '=' {'native','natural','mac68k','power','reset'} |
| 1089 | // #pragma 'options 'align' '=' {'native','natural','mac68k','power','reset'} |
Eli Friedman | 9595c7e | 2012-10-04 02:36:51 +0000 | [diff] [blame] | 1090 | static void ParseAlignPragma(Preprocessor &PP, Token &FirstTok, |
Daniel Dunbar | cbb98ed | 2010-07-31 19:17:07 +0000 | [diff] [blame] | 1091 | bool IsOptions) { |
Daniel Dunbar | 861800c | 2010-05-26 23:29:06 +0000 | [diff] [blame] | 1092 | Token Tok; |
Daniel Dunbar | cbb98ed | 2010-07-31 19:17:07 +0000 | [diff] [blame] | 1093 | |
| 1094 | if (IsOptions) { |
| 1095 | PP.Lex(Tok); |
| 1096 | if (Tok.isNot(tok::identifier) || |
| 1097 | !Tok.getIdentifierInfo()->isStr("align")) { |
| 1098 | PP.Diag(Tok.getLocation(), diag::warn_pragma_options_expected_align); |
| 1099 | return; |
| 1100 | } |
Daniel Dunbar | 861800c | 2010-05-26 23:29:06 +0000 | [diff] [blame] | 1101 | } |
Daniel Dunbar | 638e7cf | 2010-05-27 18:42:09 +0000 | [diff] [blame] | 1102 | |
Daniel Dunbar | 861800c | 2010-05-26 23:29:06 +0000 | [diff] [blame] | 1103 | PP.Lex(Tok); |
| 1104 | if (Tok.isNot(tok::equal)) { |
Daniel Dunbar | cbb98ed | 2010-07-31 19:17:07 +0000 | [diff] [blame] | 1105 | PP.Diag(Tok.getLocation(), diag::warn_pragma_align_expected_equal) |
| 1106 | << IsOptions; |
Daniel Dunbar | 861800c | 2010-05-26 23:29:06 +0000 | [diff] [blame] | 1107 | return; |
| 1108 | } |
| 1109 | |
| 1110 | PP.Lex(Tok); |
| 1111 | if (Tok.isNot(tok::identifier)) { |
| 1112 | PP.Diag(Tok.getLocation(), diag::warn_pragma_expected_identifier) |
Daniel Dunbar | cbb98ed | 2010-07-31 19:17:07 +0000 | [diff] [blame] | 1113 | << (IsOptions ? "options" : "align"); |
Daniel Dunbar | 861800c | 2010-05-26 23:29:06 +0000 | [diff] [blame] | 1114 | return; |
| 1115 | } |
| 1116 | |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 1117 | Sema::PragmaOptionsAlignKind Kind = Sema::POAK_Natural; |
Daniel Dunbar | 861800c | 2010-05-26 23:29:06 +0000 | [diff] [blame] | 1118 | const IdentifierInfo *II = Tok.getIdentifierInfo(); |
Daniel Dunbar | 638e7cf | 2010-05-27 18:42:09 +0000 | [diff] [blame] | 1119 | if (II->isStr("native")) |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 1120 | Kind = Sema::POAK_Native; |
Daniel Dunbar | 638e7cf | 2010-05-27 18:42:09 +0000 | [diff] [blame] | 1121 | else if (II->isStr("natural")) |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 1122 | Kind = Sema::POAK_Natural; |
Daniel Dunbar | 6f73914 | 2010-05-27 18:42:17 +0000 | [diff] [blame] | 1123 | else if (II->isStr("packed")) |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 1124 | Kind = Sema::POAK_Packed; |
Daniel Dunbar | 861800c | 2010-05-26 23:29:06 +0000 | [diff] [blame] | 1125 | else if (II->isStr("power")) |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 1126 | Kind = Sema::POAK_Power; |
Daniel Dunbar | 861800c | 2010-05-26 23:29:06 +0000 | [diff] [blame] | 1127 | else if (II->isStr("mac68k")) |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 1128 | Kind = Sema::POAK_Mac68k; |
Daniel Dunbar | 861800c | 2010-05-26 23:29:06 +0000 | [diff] [blame] | 1129 | else if (II->isStr("reset")) |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 1130 | Kind = Sema::POAK_Reset; |
Daniel Dunbar | 861800c | 2010-05-26 23:29:06 +0000 | [diff] [blame] | 1131 | else { |
Daniel Dunbar | cbb98ed | 2010-07-31 19:17:07 +0000 | [diff] [blame] | 1132 | PP.Diag(Tok.getLocation(), diag::warn_pragma_align_invalid_option) |
| 1133 | << IsOptions; |
Daniel Dunbar | 861800c | 2010-05-26 23:29:06 +0000 | [diff] [blame] | 1134 | return; |
| 1135 | } |
| 1136 | |
Daniel Dunbar | 861800c | 2010-05-26 23:29:06 +0000 | [diff] [blame] | 1137 | PP.Lex(Tok); |
Peter Collingbourne | 8402155 | 2011-02-28 02:37:51 +0000 | [diff] [blame] | 1138 | if (Tok.isNot(tok::eod)) { |
Daniel Dunbar | 861800c | 2010-05-26 23:29:06 +0000 | [diff] [blame] | 1139 | PP.Diag(Tok.getLocation(), diag::warn_pragma_extra_tokens_at_eol) |
Daniel Dunbar | cbb98ed | 2010-07-31 19:17:07 +0000 | [diff] [blame] | 1140 | << (IsOptions ? "options" : "align"); |
Daniel Dunbar | 861800c | 2010-05-26 23:29:06 +0000 | [diff] [blame] | 1141 | return; |
| 1142 | } |
| 1143 | |
Eli Friedman | 9595c7e | 2012-10-04 02:36:51 +0000 | [diff] [blame] | 1144 | Token *Toks = |
| 1145 | (Token*) PP.getPreprocessorAllocator().Allocate( |
| 1146 | sizeof(Token) * 1, llvm::alignOf<Token>()); |
| 1147 | new (Toks) Token(); |
| 1148 | Toks[0].startToken(); |
| 1149 | Toks[0].setKind(tok::annot_pragma_align); |
| 1150 | Toks[0].setLocation(FirstTok.getLocation()); |
| 1151 | Toks[0].setAnnotationValue(reinterpret_cast<void*>( |
| 1152 | static_cast<uintptr_t>(Kind))); |
| 1153 | PP.EnterTokenStream(Toks, 1, /*DisableMacroExpansion=*/true, |
| 1154 | /*OwnsTokens=*/false); |
Daniel Dunbar | cbb98ed | 2010-07-31 19:17:07 +0000 | [diff] [blame] | 1155 | } |
| 1156 | |
Douglas Gregor | 80c60f7 | 2010-09-09 22:45:38 +0000 | [diff] [blame] | 1157 | void PragmaAlignHandler::HandlePragma(Preprocessor &PP, |
| 1158 | PragmaIntroducerKind Introducer, |
| 1159 | Token &AlignTok) { |
Eli Friedman | 9595c7e | 2012-10-04 02:36:51 +0000 | [diff] [blame] | 1160 | ParseAlignPragma(PP, AlignTok, /*IsOptions=*/false); |
Daniel Dunbar | cbb98ed | 2010-07-31 19:17:07 +0000 | [diff] [blame] | 1161 | } |
| 1162 | |
Douglas Gregor | 80c60f7 | 2010-09-09 22:45:38 +0000 | [diff] [blame] | 1163 | void PragmaOptionsHandler::HandlePragma(Preprocessor &PP, |
| 1164 | PragmaIntroducerKind Introducer, |
| 1165 | Token &OptionsTok) { |
Eli Friedman | 9595c7e | 2012-10-04 02:36:51 +0000 | [diff] [blame] | 1166 | ParseAlignPragma(PP, OptionsTok, /*IsOptions=*/true); |
Daniel Dunbar | 861800c | 2010-05-26 23:29:06 +0000 | [diff] [blame] | 1167 | } |
| 1168 | |
Ted Kremenek | 4726d03 | 2009-03-23 22:28:25 +0000 | [diff] [blame] | 1169 | // #pragma unused(identifier) |
Douglas Gregor | 80c60f7 | 2010-09-09 22:45:38 +0000 | [diff] [blame] | 1170 | void PragmaUnusedHandler::HandlePragma(Preprocessor &PP, |
| 1171 | PragmaIntroducerKind Introducer, |
| 1172 | Token &UnusedTok) { |
Ted Kremenek | 4726d03 | 2009-03-23 22:28:25 +0000 | [diff] [blame] | 1173 | // FIXME: Should we be expanding macros here? My guess is no. |
| 1174 | SourceLocation UnusedLoc = UnusedTok.getLocation(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1175 | |
Ted Kremenek | 4726d03 | 2009-03-23 22:28:25 +0000 | [diff] [blame] | 1176 | // Lex the left '('. |
| 1177 | Token Tok; |
| 1178 | PP.Lex(Tok); |
| 1179 | if (Tok.isNot(tok::l_paren)) { |
| 1180 | PP.Diag(Tok.getLocation(), diag::warn_pragma_expected_lparen) << "unused"; |
| 1181 | return; |
| 1182 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1183 | |
Ted Kremenek | 4726d03 | 2009-03-23 22:28:25 +0000 | [diff] [blame] | 1184 | // Lex the declaration reference(s). |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 1185 | SmallVector<Token, 5> Identifiers; |
Ted Kremenek | 4726d03 | 2009-03-23 22:28:25 +0000 | [diff] [blame] | 1186 | SourceLocation RParenLoc; |
| 1187 | bool LexID = true; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1188 | |
Ted Kremenek | 4726d03 | 2009-03-23 22:28:25 +0000 | [diff] [blame] | 1189 | while (true) { |
| 1190 | PP.Lex(Tok); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1191 | |
Ted Kremenek | 4726d03 | 2009-03-23 22:28:25 +0000 | [diff] [blame] | 1192 | if (LexID) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1193 | if (Tok.is(tok::identifier)) { |
Ted Kremenek | 7a02a37 | 2009-08-03 23:24:57 +0000 | [diff] [blame] | 1194 | Identifiers.push_back(Tok); |
Ted Kremenek | 4726d03 | 2009-03-23 22:28:25 +0000 | [diff] [blame] | 1195 | LexID = false; |
| 1196 | continue; |
| 1197 | } |
| 1198 | |
Ted Kremenek | 7a02a37 | 2009-08-03 23:24:57 +0000 | [diff] [blame] | 1199 | // Illegal token! |
Ted Kremenek | 4726d03 | 2009-03-23 22:28:25 +0000 | [diff] [blame] | 1200 | PP.Diag(Tok.getLocation(), diag::warn_pragma_unused_expected_var); |
| 1201 | return; |
| 1202 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1203 | |
Ted Kremenek | 4726d03 | 2009-03-23 22:28:25 +0000 | [diff] [blame] | 1204 | // We are execting a ')' or a ','. |
| 1205 | if (Tok.is(tok::comma)) { |
| 1206 | LexID = true; |
| 1207 | continue; |
| 1208 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1209 | |
Ted Kremenek | 4726d03 | 2009-03-23 22:28:25 +0000 | [diff] [blame] | 1210 | if (Tok.is(tok::r_paren)) { |
| 1211 | RParenLoc = Tok.getLocation(); |
| 1212 | break; |
| 1213 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1214 | |
Ted Kremenek | 7a02a37 | 2009-08-03 23:24:57 +0000 | [diff] [blame] | 1215 | // Illegal token! |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 1216 | PP.Diag(Tok.getLocation(), diag::warn_pragma_expected_punc) << "unused"; |
Ted Kremenek | 4726d03 | 2009-03-23 22:28:25 +0000 | [diff] [blame] | 1217 | return; |
| 1218 | } |
Eli Friedman | 9991479 | 2009-06-05 00:49:58 +0000 | [diff] [blame] | 1219 | |
| 1220 | PP.Lex(Tok); |
Peter Collingbourne | 8402155 | 2011-02-28 02:37:51 +0000 | [diff] [blame] | 1221 | if (Tok.isNot(tok::eod)) { |
Eli Friedman | 9991479 | 2009-06-05 00:49:58 +0000 | [diff] [blame] | 1222 | PP.Diag(Tok.getLocation(), diag::warn_pragma_extra_tokens_at_eol) << |
| 1223 | "unused"; |
| 1224 | return; |
| 1225 | } |
| 1226 | |
Ted Kremenek | 4726d03 | 2009-03-23 22:28:25 +0000 | [diff] [blame] | 1227 | // Verify that we have a location for the right parenthesis. |
| 1228 | assert(RParenLoc.isValid() && "Valid '#pragma unused' must have ')'"); |
Ted Kremenek | 7a02a37 | 2009-08-03 23:24:57 +0000 | [diff] [blame] | 1229 | assert(!Identifiers.empty() && "Valid '#pragma unused' must have arguments"); |
Ted Kremenek | 4726d03 | 2009-03-23 22:28:25 +0000 | [diff] [blame] | 1230 | |
Argyrios Kyrtzidis | b918d0f | 2011-01-17 18:58:44 +0000 | [diff] [blame] | 1231 | // For each identifier token, insert into the token stream a |
| 1232 | // annot_pragma_unused token followed by the identifier token. |
| 1233 | // This allows us to cache a "#pragma unused" that occurs inside an inline |
| 1234 | // C++ member function. |
| 1235 | |
Daniel Dunbar | b093955 | 2012-02-29 01:38:22 +0000 | [diff] [blame] | 1236 | Token *Toks = |
| 1237 | (Token*) PP.getPreprocessorAllocator().Allocate( |
| 1238 | sizeof(Token) * 2 * Identifiers.size(), llvm::alignOf<Token>()); |
Argyrios Kyrtzidis | b918d0f | 2011-01-17 18:58:44 +0000 | [diff] [blame] | 1239 | for (unsigned i=0; i != Identifiers.size(); i++) { |
| 1240 | Token &pragmaUnusedTok = Toks[2*i], &idTok = Toks[2*i+1]; |
| 1241 | pragmaUnusedTok.startToken(); |
| 1242 | pragmaUnusedTok.setKind(tok::annot_pragma_unused); |
| 1243 | pragmaUnusedTok.setLocation(UnusedLoc); |
| 1244 | idTok = Identifiers[i]; |
| 1245 | } |
Daniel Dunbar | b093955 | 2012-02-29 01:38:22 +0000 | [diff] [blame] | 1246 | PP.EnterTokenStream(Toks, 2*Identifiers.size(), |
| 1247 | /*DisableMacroExpansion=*/true, /*OwnsTokens=*/false); |
Ted Kremenek | 4726d03 | 2009-03-23 22:28:25 +0000 | [diff] [blame] | 1248 | } |
Eli Friedman | 9991479 | 2009-06-05 00:49:58 +0000 | [diff] [blame] | 1249 | |
| 1250 | // #pragma weak identifier |
| 1251 | // #pragma weak identifier '=' identifier |
Douglas Gregor | 80c60f7 | 2010-09-09 22:45:38 +0000 | [diff] [blame] | 1252 | void PragmaWeakHandler::HandlePragma(Preprocessor &PP, |
| 1253 | PragmaIntroducerKind Introducer, |
| 1254 | Token &WeakTok) { |
Eli Friedman | 9991479 | 2009-06-05 00:49:58 +0000 | [diff] [blame] | 1255 | SourceLocation WeakLoc = WeakTok.getLocation(); |
| 1256 | |
| 1257 | Token Tok; |
| 1258 | PP.Lex(Tok); |
| 1259 | if (Tok.isNot(tok::identifier)) { |
| 1260 | PP.Diag(Tok.getLocation(), diag::warn_pragma_expected_identifier) << "weak"; |
| 1261 | return; |
| 1262 | } |
| 1263 | |
Eli Friedman | 9595c7e | 2012-10-04 02:36:51 +0000 | [diff] [blame] | 1264 | Token WeakName = Tok; |
| 1265 | bool HasAlias = false; |
| 1266 | Token AliasName; |
Eli Friedman | 9991479 | 2009-06-05 00:49:58 +0000 | [diff] [blame] | 1267 | |
| 1268 | PP.Lex(Tok); |
| 1269 | if (Tok.is(tok::equal)) { |
Eli Friedman | 9595c7e | 2012-10-04 02:36:51 +0000 | [diff] [blame] | 1270 | HasAlias = true; |
Eli Friedman | 9991479 | 2009-06-05 00:49:58 +0000 | [diff] [blame] | 1271 | PP.Lex(Tok); |
| 1272 | if (Tok.isNot(tok::identifier)) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1273 | PP.Diag(Tok.getLocation(), diag::warn_pragma_expected_identifier) |
Eli Friedman | 9991479 | 2009-06-05 00:49:58 +0000 | [diff] [blame] | 1274 | << "weak"; |
| 1275 | return; |
| 1276 | } |
Eli Friedman | 9595c7e | 2012-10-04 02:36:51 +0000 | [diff] [blame] | 1277 | AliasName = Tok; |
Eli Friedman | 9991479 | 2009-06-05 00:49:58 +0000 | [diff] [blame] | 1278 | PP.Lex(Tok); |
| 1279 | } |
| 1280 | |
Peter Collingbourne | 8402155 | 2011-02-28 02:37:51 +0000 | [diff] [blame] | 1281 | if (Tok.isNot(tok::eod)) { |
Eli Friedman | 9991479 | 2009-06-05 00:49:58 +0000 | [diff] [blame] | 1282 | PP.Diag(Tok.getLocation(), diag::warn_pragma_extra_tokens_at_eol) << "weak"; |
| 1283 | return; |
| 1284 | } |
| 1285 | |
Eli Friedman | 9595c7e | 2012-10-04 02:36:51 +0000 | [diff] [blame] | 1286 | if (HasAlias) { |
| 1287 | Token *Toks = |
| 1288 | (Token*) PP.getPreprocessorAllocator().Allocate( |
| 1289 | sizeof(Token) * 3, llvm::alignOf<Token>()); |
| 1290 | Token &pragmaUnusedTok = Toks[0]; |
| 1291 | pragmaUnusedTok.startToken(); |
| 1292 | pragmaUnusedTok.setKind(tok::annot_pragma_weakalias); |
| 1293 | pragmaUnusedTok.setLocation(WeakLoc); |
| 1294 | Toks[1] = WeakName; |
| 1295 | Toks[2] = AliasName; |
| 1296 | PP.EnterTokenStream(Toks, 3, |
| 1297 | /*DisableMacroExpansion=*/true, /*OwnsTokens=*/false); |
Eli Friedman | 9991479 | 2009-06-05 00:49:58 +0000 | [diff] [blame] | 1298 | } else { |
Eli Friedman | 9595c7e | 2012-10-04 02:36:51 +0000 | [diff] [blame] | 1299 | Token *Toks = |
| 1300 | (Token*) PP.getPreprocessorAllocator().Allocate( |
| 1301 | sizeof(Token) * 2, llvm::alignOf<Token>()); |
| 1302 | Token &pragmaUnusedTok = Toks[0]; |
| 1303 | pragmaUnusedTok.startToken(); |
| 1304 | pragmaUnusedTok.setKind(tok::annot_pragma_weak); |
| 1305 | pragmaUnusedTok.setLocation(WeakLoc); |
| 1306 | Toks[1] = WeakName; |
| 1307 | PP.EnterTokenStream(Toks, 2, |
| 1308 | /*DisableMacroExpansion=*/true, /*OwnsTokens=*/false); |
Eli Friedman | 9991479 | 2009-06-05 00:49:58 +0000 | [diff] [blame] | 1309 | } |
| 1310 | } |
Peter Collingbourne | 321b817 | 2011-02-14 01:42:35 +0000 | [diff] [blame] | 1311 | |
David Chisnall | 5f3c163 | 2012-02-18 16:12:34 +0000 | [diff] [blame] | 1312 | // #pragma redefine_extname identifier identifier |
| 1313 | void PragmaRedefineExtnameHandler::HandlePragma(Preprocessor &PP, |
| 1314 | PragmaIntroducerKind Introducer, |
| 1315 | Token &RedefToken) { |
| 1316 | SourceLocation RedefLoc = RedefToken.getLocation(); |
| 1317 | |
| 1318 | Token Tok; |
| 1319 | PP.Lex(Tok); |
| 1320 | if (Tok.isNot(tok::identifier)) { |
| 1321 | PP.Diag(Tok.getLocation(), diag::warn_pragma_expected_identifier) << |
| 1322 | "redefine_extname"; |
| 1323 | return; |
| 1324 | } |
| 1325 | |
Eli Friedman | 9595c7e | 2012-10-04 02:36:51 +0000 | [diff] [blame] | 1326 | Token RedefName = Tok; |
David Chisnall | 5f3c163 | 2012-02-18 16:12:34 +0000 | [diff] [blame] | 1327 | PP.Lex(Tok); |
Eli Friedman | 9595c7e | 2012-10-04 02:36:51 +0000 | [diff] [blame] | 1328 | |
David Chisnall | 5f3c163 | 2012-02-18 16:12:34 +0000 | [diff] [blame] | 1329 | if (Tok.isNot(tok::identifier)) { |
| 1330 | PP.Diag(Tok.getLocation(), diag::warn_pragma_expected_identifier) |
| 1331 | << "redefine_extname"; |
| 1332 | return; |
| 1333 | } |
Eli Friedman | 9595c7e | 2012-10-04 02:36:51 +0000 | [diff] [blame] | 1334 | |
| 1335 | Token AliasName = Tok; |
David Chisnall | 5f3c163 | 2012-02-18 16:12:34 +0000 | [diff] [blame] | 1336 | PP.Lex(Tok); |
| 1337 | |
| 1338 | if (Tok.isNot(tok::eod)) { |
| 1339 | PP.Diag(Tok.getLocation(), diag::warn_pragma_extra_tokens_at_eol) << |
| 1340 | "redefine_extname"; |
| 1341 | return; |
| 1342 | } |
| 1343 | |
Eli Friedman | 9595c7e | 2012-10-04 02:36:51 +0000 | [diff] [blame] | 1344 | Token *Toks = |
| 1345 | (Token*) PP.getPreprocessorAllocator().Allocate( |
| 1346 | sizeof(Token) * 3, llvm::alignOf<Token>()); |
| 1347 | Token &pragmaRedefTok = Toks[0]; |
| 1348 | pragmaRedefTok.startToken(); |
| 1349 | pragmaRedefTok.setKind(tok::annot_pragma_redefine_extname); |
| 1350 | pragmaRedefTok.setLocation(RedefLoc); |
| 1351 | Toks[1] = RedefName; |
| 1352 | Toks[2] = AliasName; |
| 1353 | PP.EnterTokenStream(Toks, 3, |
| 1354 | /*DisableMacroExpansion=*/true, /*OwnsTokens=*/false); |
David Chisnall | 5f3c163 | 2012-02-18 16:12:34 +0000 | [diff] [blame] | 1355 | } |
| 1356 | |
| 1357 | |
Peter Collingbourne | 321b817 | 2011-02-14 01:42:35 +0000 | [diff] [blame] | 1358 | void |
| 1359 | PragmaFPContractHandler::HandlePragma(Preprocessor &PP, |
| 1360 | PragmaIntroducerKind Introducer, |
| 1361 | Token &Tok) { |
| 1362 | tok::OnOffSwitch OOS; |
| 1363 | if (PP.LexOnOffSwitch(OOS)) |
| 1364 | return; |
| 1365 | |
Eli Friedman | 9595c7e | 2012-10-04 02:36:51 +0000 | [diff] [blame] | 1366 | Token *Toks = |
| 1367 | (Token*) PP.getPreprocessorAllocator().Allocate( |
| 1368 | sizeof(Token) * 1, llvm::alignOf<Token>()); |
| 1369 | new (Toks) Token(); |
| 1370 | Toks[0].startToken(); |
| 1371 | Toks[0].setKind(tok::annot_pragma_fp_contract); |
| 1372 | Toks[0].setLocation(Tok.getLocation()); |
| 1373 | Toks[0].setAnnotationValue(reinterpret_cast<void*>( |
| 1374 | static_cast<uintptr_t>(OOS))); |
| 1375 | PP.EnterTokenStream(Toks, 1, /*DisableMacroExpansion=*/true, |
| 1376 | /*OwnsTokens=*/false); |
Peter Collingbourne | 321b817 | 2011-02-14 01:42:35 +0000 | [diff] [blame] | 1377 | } |
Peter Collingbourne | f315fa8 | 2011-02-14 01:42:53 +0000 | [diff] [blame] | 1378 | |
| 1379 | void |
| 1380 | PragmaOpenCLExtensionHandler::HandlePragma(Preprocessor &PP, |
| 1381 | PragmaIntroducerKind Introducer, |
| 1382 | Token &Tok) { |
Tanya Lattner | b38b6a7 | 2011-04-14 23:35:31 +0000 | [diff] [blame] | 1383 | PP.LexUnexpandedToken(Tok); |
Peter Collingbourne | f315fa8 | 2011-02-14 01:42:53 +0000 | [diff] [blame] | 1384 | if (Tok.isNot(tok::identifier)) { |
| 1385 | PP.Diag(Tok.getLocation(), diag::warn_pragma_expected_identifier) << |
| 1386 | "OPENCL"; |
| 1387 | return; |
| 1388 | } |
| 1389 | IdentifierInfo *ename = Tok.getIdentifierInfo(); |
| 1390 | SourceLocation NameLoc = Tok.getLocation(); |
| 1391 | |
| 1392 | PP.Lex(Tok); |
| 1393 | if (Tok.isNot(tok::colon)) { |
| 1394 | PP.Diag(Tok.getLocation(), diag::warn_pragma_expected_colon) << ename; |
| 1395 | return; |
| 1396 | } |
| 1397 | |
| 1398 | PP.Lex(Tok); |
| 1399 | if (Tok.isNot(tok::identifier)) { |
| 1400 | PP.Diag(Tok.getLocation(), diag::warn_pragma_expected_enable_disable); |
| 1401 | return; |
| 1402 | } |
| 1403 | IdentifierInfo *op = Tok.getIdentifierInfo(); |
| 1404 | |
| 1405 | unsigned state; |
| 1406 | if (op->isStr("enable")) { |
| 1407 | state = 1; |
| 1408 | } else if (op->isStr("disable")) { |
| 1409 | state = 0; |
| 1410 | } else { |
| 1411 | PP.Diag(Tok.getLocation(), diag::warn_pragma_expected_enable_disable); |
| 1412 | return; |
| 1413 | } |
Pekka Jaaskelainen | a0950e8 | 2013-10-12 09:29:48 +0000 | [diff] [blame] | 1414 | SourceLocation StateLoc = Tok.getLocation(); |
Peter Collingbourne | f315fa8 | 2011-02-14 01:42:53 +0000 | [diff] [blame] | 1415 | |
Eli Friedman | 9595c7e | 2012-10-04 02:36:51 +0000 | [diff] [blame] | 1416 | PP.Lex(Tok); |
| 1417 | if (Tok.isNot(tok::eod)) { |
| 1418 | PP.Diag(Tok.getLocation(), diag::warn_pragma_extra_tokens_at_eol) << |
| 1419 | "OPENCL EXTENSION"; |
Peter Collingbourne | f315fa8 | 2011-02-14 01:42:53 +0000 | [diff] [blame] | 1420 | return; |
| 1421 | } |
Eli Friedman | 9595c7e | 2012-10-04 02:36:51 +0000 | [diff] [blame] | 1422 | |
| 1423 | OpenCLExtData data(ename, state); |
| 1424 | Token *Toks = |
| 1425 | (Token*) PP.getPreprocessorAllocator().Allocate( |
| 1426 | sizeof(Token) * 1, llvm::alignOf<Token>()); |
| 1427 | new (Toks) Token(); |
| 1428 | Toks[0].startToken(); |
| 1429 | Toks[0].setKind(tok::annot_pragma_opencl_extension); |
| 1430 | Toks[0].setLocation(NameLoc); |
| 1431 | Toks[0].setAnnotationValue(data.getOpaqueValue()); |
| 1432 | PP.EnterTokenStream(Toks, 1, /*DisableMacroExpansion=*/true, |
| 1433 | /*OwnsTokens=*/false); |
Pekka Jaaskelainen | a0950e8 | 2013-10-12 09:29:48 +0000 | [diff] [blame] | 1434 | |
| 1435 | if (PP.getPPCallbacks()) |
| 1436 | PP.getPPCallbacks()->PragmaOpenCLExtension(NameLoc, ename, |
| 1437 | StateLoc, state); |
Peter Collingbourne | f315fa8 | 2011-02-14 01:42:53 +0000 | [diff] [blame] | 1438 | } |
| 1439 | |
Alexey Bataev | c640058 | 2013-03-22 06:34:35 +0000 | [diff] [blame] | 1440 | /// \brief Handle '#pragma omp ...' when OpenMP is disabled. |
| 1441 | /// |
| 1442 | void |
| 1443 | PragmaNoOpenMPHandler::HandlePragma(Preprocessor &PP, |
| 1444 | PragmaIntroducerKind Introducer, |
| 1445 | Token &FirstTok) { |
Stephen Hines | c568f1e | 2014-07-21 00:47:37 -0700 | [diff] [blame] | 1446 | if (!PP.getDiagnostics().isIgnored(diag::warn_pragma_omp_ignored, |
| 1447 | FirstTok.getLocation())) { |
Alexey Bataev | c640058 | 2013-03-22 06:34:35 +0000 | [diff] [blame] | 1448 | PP.Diag(FirstTok, diag::warn_pragma_omp_ignored); |
Stephen Hines | c568f1e | 2014-07-21 00:47:37 -0700 | [diff] [blame] | 1449 | PP.getDiagnostics().setSeverity(diag::warn_pragma_omp_ignored, |
| 1450 | diag::Severity::Ignored, SourceLocation()); |
Alexey Bataev | c640058 | 2013-03-22 06:34:35 +0000 | [diff] [blame] | 1451 | } |
| 1452 | PP.DiscardUntilEndOfDirective(); |
| 1453 | } |
| 1454 | |
| 1455 | /// \brief Handle '#pragma omp ...' when OpenMP is enabled. |
| 1456 | /// |
| 1457 | void |
| 1458 | PragmaOpenMPHandler::HandlePragma(Preprocessor &PP, |
| 1459 | PragmaIntroducerKind Introducer, |
| 1460 | Token &FirstTok) { |
| 1461 | SmallVector<Token, 16> Pragma; |
| 1462 | Token Tok; |
| 1463 | Tok.startToken(); |
| 1464 | Tok.setKind(tok::annot_pragma_openmp); |
| 1465 | Tok.setLocation(FirstTok.getLocation()); |
| 1466 | |
| 1467 | while (Tok.isNot(tok::eod)) { |
| 1468 | Pragma.push_back(Tok); |
| 1469 | PP.Lex(Tok); |
| 1470 | } |
| 1471 | SourceLocation EodLoc = Tok.getLocation(); |
| 1472 | Tok.startToken(); |
| 1473 | Tok.setKind(tok::annot_pragma_openmp_end); |
| 1474 | Tok.setLocation(EodLoc); |
| 1475 | Pragma.push_back(Tok); |
| 1476 | |
| 1477 | Token *Toks = new Token[Pragma.size()]; |
| 1478 | std::copy(Pragma.begin(), Pragma.end(), Toks); |
| 1479 | PP.EnterTokenStream(Toks, Pragma.size(), |
| 1480 | /*DisableMacroExpansion=*/true, /*OwnsTokens=*/true); |
| 1481 | } |
Reid Kleckner | 7adf79a | 2013-05-06 21:02:12 +0000 | [diff] [blame] | 1482 | |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 1483 | /// \brief Handle '#pragma pointers_to_members' |
| 1484 | // The grammar for this pragma is as follows: |
| 1485 | // |
| 1486 | // <inheritance model> ::= ('single' | 'multiple' | 'virtual') '_inheritance' |
| 1487 | // |
| 1488 | // #pragma pointers_to_members '(' 'best_case' ')' |
| 1489 | // #pragma pointers_to_members '(' 'full_generality' [',' inheritance-model] ')' |
| 1490 | // #pragma pointers_to_members '(' inheritance-model ')' |
| 1491 | void PragmaMSPointersToMembers::HandlePragma(Preprocessor &PP, |
| 1492 | PragmaIntroducerKind Introducer, |
| 1493 | Token &Tok) { |
| 1494 | SourceLocation PointersToMembersLoc = Tok.getLocation(); |
| 1495 | PP.Lex(Tok); |
| 1496 | if (Tok.isNot(tok::l_paren)) { |
| 1497 | PP.Diag(PointersToMembersLoc, diag::warn_pragma_expected_lparen) |
| 1498 | << "pointers_to_members"; |
| 1499 | return; |
| 1500 | } |
| 1501 | PP.Lex(Tok); |
| 1502 | const IdentifierInfo *Arg = Tok.getIdentifierInfo(); |
| 1503 | if (!Arg) { |
| 1504 | PP.Diag(Tok.getLocation(), diag::warn_pragma_expected_identifier) |
| 1505 | << "pointers_to_members"; |
| 1506 | return; |
| 1507 | } |
| 1508 | PP.Lex(Tok); |
| 1509 | |
| 1510 | LangOptions::PragmaMSPointersToMembersKind RepresentationMethod; |
| 1511 | if (Arg->isStr("best_case")) { |
| 1512 | RepresentationMethod = LangOptions::PPTMK_BestCase; |
| 1513 | } else { |
| 1514 | if (Arg->isStr("full_generality")) { |
| 1515 | if (Tok.is(tok::comma)) { |
| 1516 | PP.Lex(Tok); |
| 1517 | |
| 1518 | Arg = Tok.getIdentifierInfo(); |
| 1519 | if (!Arg) { |
| 1520 | PP.Diag(Tok.getLocation(), |
| 1521 | diag::err_pragma_pointers_to_members_unknown_kind) |
| 1522 | << Tok.getKind() << /*OnlyInheritanceModels*/ 0; |
| 1523 | return; |
| 1524 | } |
| 1525 | PP.Lex(Tok); |
| 1526 | } else if (Tok.is(tok::r_paren)) { |
| 1527 | // #pragma pointers_to_members(full_generality) implicitly specifies |
| 1528 | // virtual_inheritance. |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame] | 1529 | Arg = nullptr; |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 1530 | RepresentationMethod = LangOptions::PPTMK_FullGeneralityVirtualInheritance; |
| 1531 | } else { |
| 1532 | PP.Diag(Tok.getLocation(), diag::err_expected_punc) |
| 1533 | << "full_generality"; |
| 1534 | return; |
| 1535 | } |
| 1536 | } |
| 1537 | |
| 1538 | if (Arg) { |
| 1539 | if (Arg->isStr("single_inheritance")) { |
| 1540 | RepresentationMethod = |
| 1541 | LangOptions::PPTMK_FullGeneralitySingleInheritance; |
| 1542 | } else if (Arg->isStr("multiple_inheritance")) { |
| 1543 | RepresentationMethod = |
| 1544 | LangOptions::PPTMK_FullGeneralityMultipleInheritance; |
| 1545 | } else if (Arg->isStr("virtual_inheritance")) { |
| 1546 | RepresentationMethod = |
| 1547 | LangOptions::PPTMK_FullGeneralityVirtualInheritance; |
| 1548 | } else { |
| 1549 | PP.Diag(Tok.getLocation(), |
| 1550 | diag::err_pragma_pointers_to_members_unknown_kind) |
| 1551 | << Arg << /*HasPointerDeclaration*/ 1; |
| 1552 | return; |
| 1553 | } |
| 1554 | } |
| 1555 | } |
| 1556 | |
| 1557 | if (Tok.isNot(tok::r_paren)) { |
| 1558 | PP.Diag(Tok.getLocation(), diag::err_expected_rparen_after) |
| 1559 | << (Arg ? Arg->getName() : "full_generality"); |
| 1560 | return; |
| 1561 | } |
| 1562 | |
| 1563 | PP.Lex(Tok); |
| 1564 | if (Tok.isNot(tok::eod)) { |
| 1565 | PP.Diag(Tok.getLocation(), diag::warn_pragma_extra_tokens_at_eol) |
| 1566 | << "pointers_to_members"; |
| 1567 | return; |
| 1568 | } |
| 1569 | |
| 1570 | Token AnnotTok; |
| 1571 | AnnotTok.startToken(); |
| 1572 | AnnotTok.setKind(tok::annot_pragma_ms_pointers_to_members); |
| 1573 | AnnotTok.setLocation(PointersToMembersLoc); |
| 1574 | AnnotTok.setAnnotationValue( |
| 1575 | reinterpret_cast<void *>(static_cast<uintptr_t>(RepresentationMethod))); |
| 1576 | PP.EnterToken(AnnotTok); |
| 1577 | } |
| 1578 | |
| 1579 | /// \brief Handle '#pragma vtordisp' |
| 1580 | // The grammar for this pragma is as follows: |
| 1581 | // |
| 1582 | // <vtordisp-mode> ::= ('off' | 'on' | '0' | '1' | '2' ) |
| 1583 | // |
| 1584 | // #pragma vtordisp '(' ['push' ','] vtordisp-mode ')' |
| 1585 | // #pragma vtordisp '(' 'pop' ')' |
| 1586 | // #pragma vtordisp '(' ')' |
| 1587 | void PragmaMSVtorDisp::HandlePragma(Preprocessor &PP, |
| 1588 | PragmaIntroducerKind Introducer, |
| 1589 | Token &Tok) { |
| 1590 | SourceLocation VtorDispLoc = Tok.getLocation(); |
| 1591 | PP.Lex(Tok); |
| 1592 | if (Tok.isNot(tok::l_paren)) { |
| 1593 | PP.Diag(VtorDispLoc, diag::warn_pragma_expected_lparen) << "vtordisp"; |
| 1594 | return; |
| 1595 | } |
| 1596 | PP.Lex(Tok); |
| 1597 | |
| 1598 | Sema::PragmaVtorDispKind Kind = Sema::PVDK_Set; |
| 1599 | const IdentifierInfo *II = Tok.getIdentifierInfo(); |
| 1600 | if (II) { |
| 1601 | if (II->isStr("push")) { |
| 1602 | // #pragma vtordisp(push, mode) |
| 1603 | PP.Lex(Tok); |
| 1604 | if (Tok.isNot(tok::comma)) { |
| 1605 | PP.Diag(VtorDispLoc, diag::warn_pragma_expected_punc) << "vtordisp"; |
| 1606 | return; |
| 1607 | } |
| 1608 | PP.Lex(Tok); |
| 1609 | Kind = Sema::PVDK_Push; |
| 1610 | // not push, could be on/off |
| 1611 | } else if (II->isStr("pop")) { |
| 1612 | // #pragma vtordisp(pop) |
| 1613 | PP.Lex(Tok); |
| 1614 | Kind = Sema::PVDK_Pop; |
| 1615 | } |
| 1616 | // not push or pop, could be on/off |
| 1617 | } else { |
| 1618 | if (Tok.is(tok::r_paren)) { |
| 1619 | // #pragma vtordisp() |
| 1620 | Kind = Sema::PVDK_Reset; |
| 1621 | } |
| 1622 | } |
| 1623 | |
| 1624 | |
| 1625 | uint64_t Value = 0; |
| 1626 | if (Kind == Sema::PVDK_Push || Kind == Sema::PVDK_Set) { |
| 1627 | const IdentifierInfo *II = Tok.getIdentifierInfo(); |
| 1628 | if (II && II->isStr("off")) { |
| 1629 | PP.Lex(Tok); |
| 1630 | Value = 0; |
| 1631 | } else if (II && II->isStr("on")) { |
| 1632 | PP.Lex(Tok); |
| 1633 | Value = 1; |
| 1634 | } else if (Tok.is(tok::numeric_constant) && |
| 1635 | PP.parseSimpleIntegerLiteral(Tok, Value)) { |
| 1636 | if (Value > 2) { |
| 1637 | PP.Diag(Tok.getLocation(), diag::warn_pragma_expected_integer) |
| 1638 | << 0 << 2 << "vtordisp"; |
| 1639 | return; |
| 1640 | } |
| 1641 | } else { |
| 1642 | PP.Diag(Tok.getLocation(), diag::warn_pragma_invalid_action) |
| 1643 | << "vtordisp"; |
| 1644 | return; |
| 1645 | } |
| 1646 | } |
| 1647 | |
| 1648 | // Finish the pragma: ')' $ |
| 1649 | if (Tok.isNot(tok::r_paren)) { |
| 1650 | PP.Diag(VtorDispLoc, diag::warn_pragma_expected_rparen) << "vtordisp"; |
| 1651 | return; |
| 1652 | } |
| 1653 | PP.Lex(Tok); |
| 1654 | if (Tok.isNot(tok::eod)) { |
| 1655 | PP.Diag(Tok.getLocation(), diag::warn_pragma_extra_tokens_at_eol) |
| 1656 | << "vtordisp"; |
| 1657 | return; |
| 1658 | } |
| 1659 | |
| 1660 | // Enter the annotation. |
| 1661 | Token AnnotTok; |
| 1662 | AnnotTok.startToken(); |
| 1663 | AnnotTok.setKind(tok::annot_pragma_ms_vtordisp); |
| 1664 | AnnotTok.setLocation(VtorDispLoc); |
| 1665 | AnnotTok.setAnnotationValue(reinterpret_cast<void *>( |
| 1666 | static_cast<uintptr_t>((Kind << 16) | (Value & 0xFFFF)))); |
| 1667 | PP.EnterToken(AnnotTok); |
| 1668 | } |
| 1669 | |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame] | 1670 | /// \brief Handle all MS pragmas. Simply forwards the tokens after inserting |
| 1671 | /// an annotation token. |
| 1672 | void PragmaMSPragma::HandlePragma(Preprocessor &PP, |
| 1673 | PragmaIntroducerKind Introducer, |
| 1674 | Token &Tok) { |
| 1675 | Token EoF, AnnotTok; |
| 1676 | EoF.startToken(); |
| 1677 | EoF.setKind(tok::eof); |
| 1678 | AnnotTok.startToken(); |
| 1679 | AnnotTok.setKind(tok::annot_pragma_ms_pragma); |
| 1680 | AnnotTok.setLocation(Tok.getLocation()); |
| 1681 | SmallVector<Token, 8> TokenVector; |
| 1682 | // Suck up all of the tokens before the eod. |
| 1683 | for (; Tok.isNot(tok::eod); PP.Lex(Tok)) |
| 1684 | TokenVector.push_back(Tok); |
| 1685 | // Add a sentinal EoF token to the end of the list. |
| 1686 | TokenVector.push_back(EoF); |
| 1687 | // We must allocate this array with new because EnterTokenStream is going to |
| 1688 | // delete it later. |
| 1689 | Token *TokenArray = new Token[TokenVector.size()]; |
| 1690 | std::copy(TokenVector.begin(), TokenVector.end(), TokenArray); |
| 1691 | auto Value = new (PP.getPreprocessorAllocator()) |
| 1692 | std::pair<Token*, size_t>(std::make_pair(TokenArray, TokenVector.size())); |
| 1693 | AnnotTok.setAnnotationValue(Value); |
| 1694 | PP.EnterToken(AnnotTok); |
| 1695 | } |
| 1696 | |
Aaron Ballman | a7ff62f | 2013-06-04 02:07:14 +0000 | [diff] [blame] | 1697 | /// \brief Handle the Microsoft \#pragma detect_mismatch extension. |
| 1698 | /// |
| 1699 | /// The syntax is: |
| 1700 | /// \code |
| 1701 | /// #pragma detect_mismatch("name", "value") |
| 1702 | /// \endcode |
| 1703 | /// Where 'name' and 'value' are quoted strings. The values are embedded in |
| 1704 | /// the object file and passed along to the linker. If the linker detects a |
| 1705 | /// mismatch in the object file's values for the given name, a LNK2038 error |
| 1706 | /// is emitted. See MSDN for more details. |
| 1707 | void PragmaDetectMismatchHandler::HandlePragma(Preprocessor &PP, |
| 1708 | PragmaIntroducerKind Introducer, |
| 1709 | Token &Tok) { |
| 1710 | SourceLocation CommentLoc = Tok.getLocation(); |
| 1711 | PP.Lex(Tok); |
| 1712 | if (Tok.isNot(tok::l_paren)) { |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 1713 | PP.Diag(CommentLoc, diag::err_expected) << tok::l_paren; |
Aaron Ballman | a7ff62f | 2013-06-04 02:07:14 +0000 | [diff] [blame] | 1714 | return; |
| 1715 | } |
| 1716 | |
| 1717 | // Read the name to embed, which must be a string literal. |
| 1718 | std::string NameString; |
| 1719 | if (!PP.LexStringLiteral(Tok, NameString, |
| 1720 | "pragma detect_mismatch", |
| 1721 | /*MacroExpansion=*/true)) |
| 1722 | return; |
| 1723 | |
| 1724 | // Read the comma followed by a second string literal. |
| 1725 | std::string ValueString; |
| 1726 | if (Tok.isNot(tok::comma)) { |
| 1727 | PP.Diag(Tok.getLocation(), diag::err_pragma_detect_mismatch_malformed); |
| 1728 | return; |
| 1729 | } |
| 1730 | |
| 1731 | if (!PP.LexStringLiteral(Tok, ValueString, "pragma detect_mismatch", |
| 1732 | /*MacroExpansion=*/true)) |
| 1733 | return; |
| 1734 | |
| 1735 | if (Tok.isNot(tok::r_paren)) { |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 1736 | PP.Diag(Tok.getLocation(), diag::err_expected) << tok::r_paren; |
Aaron Ballman | a7ff62f | 2013-06-04 02:07:14 +0000 | [diff] [blame] | 1737 | return; |
| 1738 | } |
| 1739 | PP.Lex(Tok); // Eat the r_paren. |
| 1740 | |
| 1741 | if (Tok.isNot(tok::eod)) { |
| 1742 | PP.Diag(Tok.getLocation(), diag::err_pragma_detect_mismatch_malformed); |
| 1743 | return; |
| 1744 | } |
| 1745 | |
| 1746 | // If the pragma is lexically sound, notify any interested PPCallbacks. |
| 1747 | if (PP.getPPCallbacks()) |
| 1748 | PP.getPPCallbacks()->PragmaDetectMismatch(CommentLoc, NameString, |
| 1749 | ValueString); |
| 1750 | |
| 1751 | Actions.ActOnPragmaDetectMismatch(NameString, ValueString); |
| 1752 | } |
| 1753 | |
Reid Kleckner | 7adf79a | 2013-05-06 21:02:12 +0000 | [diff] [blame] | 1754 | /// \brief Handle the microsoft \#pragma comment extension. |
| 1755 | /// |
| 1756 | /// The syntax is: |
| 1757 | /// \code |
| 1758 | /// #pragma comment(linker, "foo") |
| 1759 | /// \endcode |
| 1760 | /// 'linker' is one of five identifiers: compiler, exestr, lib, linker, user. |
| 1761 | /// "foo" is a string, which is fully macro expanded, and permits string |
| 1762 | /// concatenation, embedded escape characters etc. See MSDN for more details. |
| 1763 | void PragmaCommentHandler::HandlePragma(Preprocessor &PP, |
| 1764 | PragmaIntroducerKind Introducer, |
| 1765 | Token &Tok) { |
| 1766 | SourceLocation CommentLoc = Tok.getLocation(); |
| 1767 | PP.Lex(Tok); |
| 1768 | if (Tok.isNot(tok::l_paren)) { |
| 1769 | PP.Diag(CommentLoc, diag::err_pragma_comment_malformed); |
| 1770 | return; |
| 1771 | } |
| 1772 | |
| 1773 | // Read the identifier. |
| 1774 | PP.Lex(Tok); |
| 1775 | if (Tok.isNot(tok::identifier)) { |
| 1776 | PP.Diag(CommentLoc, diag::err_pragma_comment_malformed); |
| 1777 | return; |
| 1778 | } |
| 1779 | |
| 1780 | // Verify that this is one of the 5 whitelisted options. |
Reid Kleckner | 3190ca9 | 2013-05-08 13:44:39 +0000 | [diff] [blame] | 1781 | IdentifierInfo *II = Tok.getIdentifierInfo(); |
| 1782 | Sema::PragmaMSCommentKind Kind = |
| 1783 | llvm::StringSwitch<Sema::PragmaMSCommentKind>(II->getName()) |
| 1784 | .Case("linker", Sema::PCK_Linker) |
| 1785 | .Case("lib", Sema::PCK_Lib) |
| 1786 | .Case("compiler", Sema::PCK_Compiler) |
| 1787 | .Case("exestr", Sema::PCK_ExeStr) |
| 1788 | .Case("user", Sema::PCK_User) |
| 1789 | .Default(Sema::PCK_Unknown); |
| 1790 | if (Kind == Sema::PCK_Unknown) { |
Reid Kleckner | 7adf79a | 2013-05-06 21:02:12 +0000 | [diff] [blame] | 1791 | PP.Diag(Tok.getLocation(), diag::err_pragma_comment_unknown_kind); |
| 1792 | return; |
| 1793 | } |
| 1794 | |
| 1795 | // Read the optional string if present. |
| 1796 | PP.Lex(Tok); |
| 1797 | std::string ArgumentString; |
| 1798 | if (Tok.is(tok::comma) && !PP.LexStringLiteral(Tok, ArgumentString, |
| 1799 | "pragma comment", |
| 1800 | /*MacroExpansion=*/true)) |
| 1801 | return; |
| 1802 | |
Reid Kleckner | 3190ca9 | 2013-05-08 13:44:39 +0000 | [diff] [blame] | 1803 | // FIXME: warn that 'exestr' is deprecated. |
Reid Kleckner | 7adf79a | 2013-05-06 21:02:12 +0000 | [diff] [blame] | 1804 | // FIXME: If the kind is "compiler" warn if the string is present (it is |
| 1805 | // ignored). |
Reid Kleckner | 3190ca9 | 2013-05-08 13:44:39 +0000 | [diff] [blame] | 1806 | // The MSDN docs say that "lib" and "linker" require a string and have a short |
| 1807 | // whitelist of linker options they support, but in practice MSVC doesn't |
| 1808 | // issue a diagnostic. Therefore neither does clang. |
Reid Kleckner | 7adf79a | 2013-05-06 21:02:12 +0000 | [diff] [blame] | 1809 | |
| 1810 | if (Tok.isNot(tok::r_paren)) { |
| 1811 | PP.Diag(Tok.getLocation(), diag::err_pragma_comment_malformed); |
| 1812 | return; |
| 1813 | } |
| 1814 | PP.Lex(Tok); // eat the r_paren. |
| 1815 | |
| 1816 | if (Tok.isNot(tok::eod)) { |
| 1817 | PP.Diag(Tok.getLocation(), diag::err_pragma_comment_malformed); |
| 1818 | return; |
| 1819 | } |
| 1820 | |
| 1821 | // If the pragma is lexically sound, notify any interested PPCallbacks. |
| 1822 | if (PP.getPPCallbacks()) |
| 1823 | PP.getPPCallbacks()->PragmaComment(CommentLoc, II, ArgumentString); |
Reid Kleckner | 3190ca9 | 2013-05-08 13:44:39 +0000 | [diff] [blame] | 1824 | |
| 1825 | Actions.ActOnPragmaMSComment(Kind, ArgumentString); |
Reid Kleckner | 7adf79a | 2013-05-06 21:02:12 +0000 | [diff] [blame] | 1826 | } |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame] | 1827 | |
| 1828 | // #pragma clang optimize off |
| 1829 | // #pragma clang optimize on |
| 1830 | void PragmaOptimizeHandler::HandlePragma(Preprocessor &PP, |
| 1831 | PragmaIntroducerKind Introducer, |
| 1832 | Token &FirstToken) { |
| 1833 | Token Tok; |
| 1834 | PP.Lex(Tok); |
| 1835 | if (Tok.is(tok::eod)) { |
Stephen Hines | 176edba | 2014-12-01 14:53:08 -0800 | [diff] [blame] | 1836 | PP.Diag(Tok.getLocation(), diag::err_pragma_missing_argument) |
| 1837 | << "clang optimize" << /*Expected=*/true << "'on' or 'off'"; |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame] | 1838 | return; |
| 1839 | } |
| 1840 | if (Tok.isNot(tok::identifier)) { |
| 1841 | PP.Diag(Tok.getLocation(), diag::err_pragma_optimize_invalid_argument) |
| 1842 | << PP.getSpelling(Tok); |
| 1843 | return; |
| 1844 | } |
| 1845 | const IdentifierInfo *II = Tok.getIdentifierInfo(); |
| 1846 | // The only accepted values are 'on' or 'off'. |
| 1847 | bool IsOn = false; |
| 1848 | if (II->isStr("on")) { |
| 1849 | IsOn = true; |
| 1850 | } else if (!II->isStr("off")) { |
| 1851 | PP.Diag(Tok.getLocation(), diag::err_pragma_optimize_invalid_argument) |
| 1852 | << PP.getSpelling(Tok); |
| 1853 | return; |
| 1854 | } |
| 1855 | PP.Lex(Tok); |
| 1856 | |
| 1857 | if (Tok.isNot(tok::eod)) { |
| 1858 | PP.Diag(Tok.getLocation(), diag::err_pragma_optimize_extra_argument) |
| 1859 | << PP.getSpelling(Tok); |
| 1860 | return; |
| 1861 | } |
| 1862 | |
| 1863 | Actions.ActOnPragmaOptimize(IsOn, FirstToken.getLocation()); |
| 1864 | } |
Stephen Hines | c568f1e | 2014-07-21 00:47:37 -0700 | [diff] [blame] | 1865 | |
Stephen Hines | 176edba | 2014-12-01 14:53:08 -0800 | [diff] [blame] | 1866 | /// \brief Parses loop or unroll pragma hint value and fills in Info. |
| 1867 | static bool ParseLoopHintValue(Preprocessor &PP, Token &Tok, Token PragmaName, |
| 1868 | Token Option, bool ValueInParens, |
| 1869 | PragmaLoopHintInfo &Info) { |
| 1870 | SmallVector<Token, 1> ValueList; |
| 1871 | int OpenParens = ValueInParens ? 1 : 0; |
| 1872 | // Read constant expression. |
| 1873 | while (Tok.isNot(tok::eod)) { |
| 1874 | if (Tok.is(tok::l_paren)) |
| 1875 | OpenParens++; |
| 1876 | else if (Tok.is(tok::r_paren)) { |
| 1877 | OpenParens--; |
| 1878 | if (OpenParens == 0 && ValueInParens) |
| 1879 | break; |
| 1880 | } |
| 1881 | |
| 1882 | ValueList.push_back(Tok); |
| 1883 | PP.Lex(Tok); |
| 1884 | } |
| 1885 | |
| 1886 | if (ValueInParens) { |
| 1887 | // Read ')' |
| 1888 | if (Tok.isNot(tok::r_paren)) { |
| 1889 | PP.Diag(Tok.getLocation(), diag::err_expected) << tok::r_paren; |
| 1890 | return true; |
| 1891 | } |
| 1892 | PP.Lex(Tok); |
| 1893 | } |
| 1894 | |
| 1895 | Token EOFTok; |
| 1896 | EOFTok.startToken(); |
| 1897 | EOFTok.setKind(tok::eof); |
| 1898 | EOFTok.setLocation(Tok.getLocation()); |
| 1899 | ValueList.push_back(EOFTok); // Terminates expression for parsing. |
| 1900 | |
| 1901 | Token *TokenArray = (Token *)PP.getPreprocessorAllocator().Allocate( |
| 1902 | ValueList.size() * sizeof(Token), llvm::alignOf<Token>()); |
| 1903 | std::copy(ValueList.begin(), ValueList.end(), TokenArray); |
| 1904 | Info.Toks = TokenArray; |
| 1905 | Info.TokSize = ValueList.size(); |
| 1906 | |
| 1907 | Info.PragmaName = PragmaName; |
| 1908 | Info.Option = Option; |
| 1909 | return false; |
| 1910 | } |
| 1911 | |
Stephen Hines | c568f1e | 2014-07-21 00:47:37 -0700 | [diff] [blame] | 1912 | /// \brief Handle the \#pragma clang loop directive. |
| 1913 | /// #pragma clang 'loop' loop-hints |
| 1914 | /// |
| 1915 | /// loop-hints: |
| 1916 | /// loop-hint loop-hints[opt] |
| 1917 | /// |
| 1918 | /// loop-hint: |
| 1919 | /// 'vectorize' '(' loop-hint-keyword ')' |
| 1920 | /// 'interleave' '(' loop-hint-keyword ')' |
Stephen Hines | 176edba | 2014-12-01 14:53:08 -0800 | [diff] [blame] | 1921 | /// 'unroll' '(' unroll-hint-keyword ')' |
Stephen Hines | c568f1e | 2014-07-21 00:47:37 -0700 | [diff] [blame] | 1922 | /// 'vectorize_width' '(' loop-hint-value ')' |
| 1923 | /// 'interleave_count' '(' loop-hint-value ')' |
| 1924 | /// 'unroll_count' '(' loop-hint-value ')' |
| 1925 | /// |
| 1926 | /// loop-hint-keyword: |
| 1927 | /// 'enable' |
| 1928 | /// 'disable' |
| 1929 | /// |
Stephen Hines | 176edba | 2014-12-01 14:53:08 -0800 | [diff] [blame] | 1930 | /// unroll-hint-keyword: |
| 1931 | /// 'full' |
| 1932 | /// 'disable' |
| 1933 | /// |
Stephen Hines | c568f1e | 2014-07-21 00:47:37 -0700 | [diff] [blame] | 1934 | /// loop-hint-value: |
| 1935 | /// constant-expression |
| 1936 | /// |
| 1937 | /// Specifying vectorize(enable) or vectorize_width(_value_) instructs llvm to |
| 1938 | /// try vectorizing the instructions of the loop it precedes. Specifying |
| 1939 | /// interleave(enable) or interleave_count(_value_) instructs llvm to try |
| 1940 | /// interleaving multiple iterations of the loop it precedes. The width of the |
| 1941 | /// vector instructions is specified by vectorize_width() and the number of |
| 1942 | /// interleaved loop iterations is specified by interleave_count(). Specifying a |
| 1943 | /// value of 1 effectively disables vectorization/interleaving, even if it is |
| 1944 | /// possible and profitable, and 0 is invalid. The loop vectorizer currently |
| 1945 | /// only works on inner loops. |
| 1946 | /// |
| 1947 | /// The unroll and unroll_count directives control the concatenation |
Stephen Hines | 176edba | 2014-12-01 14:53:08 -0800 | [diff] [blame] | 1948 | /// unroller. Specifying unroll(full) instructs llvm to try to |
Stephen Hines | c568f1e | 2014-07-21 00:47:37 -0700 | [diff] [blame] | 1949 | /// unroll the loop completely, and unroll(disable) disables unrolling |
| 1950 | /// for the loop. Specifying unroll_count(_value_) instructs llvm to |
| 1951 | /// try to unroll the loop the number of times indicated by the value. |
Stephen Hines | c568f1e | 2014-07-21 00:47:37 -0700 | [diff] [blame] | 1952 | void PragmaLoopHintHandler::HandlePragma(Preprocessor &PP, |
| 1953 | PragmaIntroducerKind Introducer, |
| 1954 | Token &Tok) { |
Stephen Hines | 176edba | 2014-12-01 14:53:08 -0800 | [diff] [blame] | 1955 | // Incoming token is "loop" from "#pragma clang loop". |
| 1956 | Token PragmaName = Tok; |
Stephen Hines | c568f1e | 2014-07-21 00:47:37 -0700 | [diff] [blame] | 1957 | SmallVector<Token, 1> TokenList; |
| 1958 | |
| 1959 | // Lex the optimization option and verify it is an identifier. |
| 1960 | PP.Lex(Tok); |
| 1961 | if (Tok.isNot(tok::identifier)) { |
| 1962 | PP.Diag(Tok.getLocation(), diag::err_pragma_loop_invalid_option) |
| 1963 | << /*MissingOption=*/true << ""; |
| 1964 | return; |
| 1965 | } |
| 1966 | |
| 1967 | while (Tok.is(tok::identifier)) { |
| 1968 | Token Option = Tok; |
| 1969 | IdentifierInfo *OptionInfo = Tok.getIdentifierInfo(); |
| 1970 | |
| 1971 | bool OptionValid = llvm::StringSwitch<bool>(OptionInfo->getName()) |
Stephen Hines | 176edba | 2014-12-01 14:53:08 -0800 | [diff] [blame] | 1972 | .Case("vectorize", true) |
| 1973 | .Case("interleave", true) |
| 1974 | .Case("unroll", true) |
| 1975 | .Case("vectorize_width", true) |
| 1976 | .Case("interleave_count", true) |
| 1977 | .Case("unroll_count", true) |
| 1978 | .Default(false); |
Stephen Hines | c568f1e | 2014-07-21 00:47:37 -0700 | [diff] [blame] | 1979 | if (!OptionValid) { |
| 1980 | PP.Diag(Tok.getLocation(), diag::err_pragma_loop_invalid_option) |
| 1981 | << /*MissingOption=*/false << OptionInfo; |
| 1982 | return; |
| 1983 | } |
Stephen Hines | 176edba | 2014-12-01 14:53:08 -0800 | [diff] [blame] | 1984 | PP.Lex(Tok); |
Stephen Hines | c568f1e | 2014-07-21 00:47:37 -0700 | [diff] [blame] | 1985 | |
| 1986 | // Read '(' |
Stephen Hines | c568f1e | 2014-07-21 00:47:37 -0700 | [diff] [blame] | 1987 | if (Tok.isNot(tok::l_paren)) { |
| 1988 | PP.Diag(Tok.getLocation(), diag::err_expected) << tok::l_paren; |
| 1989 | return; |
| 1990 | } |
Stephen Hines | c568f1e | 2014-07-21 00:47:37 -0700 | [diff] [blame] | 1991 | PP.Lex(Tok); |
| 1992 | |
| 1993 | auto *Info = new (PP.getPreprocessorAllocator()) PragmaLoopHintInfo; |
Stephen Hines | 176edba | 2014-12-01 14:53:08 -0800 | [diff] [blame] | 1994 | if (ParseLoopHintValue(PP, Tok, PragmaName, Option, /*ValueInParens=*/true, |
| 1995 | *Info)) |
| 1996 | return; |
Stephen Hines | c568f1e | 2014-07-21 00:47:37 -0700 | [diff] [blame] | 1997 | |
Stephen Hines | 176edba | 2014-12-01 14:53:08 -0800 | [diff] [blame] | 1998 | // Generate the loop hint token. |
Stephen Hines | c568f1e | 2014-07-21 00:47:37 -0700 | [diff] [blame] | 1999 | Token LoopHintTok; |
| 2000 | LoopHintTok.startToken(); |
| 2001 | LoopHintTok.setKind(tok::annot_pragma_loop_hint); |
Stephen Hines | 176edba | 2014-12-01 14:53:08 -0800 | [diff] [blame] | 2002 | LoopHintTok.setLocation(PragmaName.getLocation()); |
Stephen Hines | c568f1e | 2014-07-21 00:47:37 -0700 | [diff] [blame] | 2003 | LoopHintTok.setAnnotationValue(static_cast<void *>(Info)); |
| 2004 | TokenList.push_back(LoopHintTok); |
| 2005 | } |
| 2006 | |
| 2007 | if (Tok.isNot(tok::eod)) { |
| 2008 | PP.Diag(Tok.getLocation(), diag::warn_pragma_extra_tokens_at_eol) |
| 2009 | << "clang loop"; |
| 2010 | return; |
| 2011 | } |
| 2012 | |
| 2013 | Token *TokenArray = new Token[TokenList.size()]; |
| 2014 | std::copy(TokenList.begin(), TokenList.end(), TokenArray); |
| 2015 | |
| 2016 | PP.EnterTokenStream(TokenArray, TokenList.size(), |
| 2017 | /*DisableMacroExpansion=*/false, |
| 2018 | /*OwnsTokens=*/true); |
| 2019 | } |
Stephen Hines | 176edba | 2014-12-01 14:53:08 -0800 | [diff] [blame] | 2020 | |
| 2021 | /// \brief Handle the loop unroll optimization pragmas. |
| 2022 | /// #pragma unroll |
| 2023 | /// #pragma unroll unroll-hint-value |
| 2024 | /// #pragma unroll '(' unroll-hint-value ')' |
| 2025 | /// #pragma nounroll |
| 2026 | /// |
| 2027 | /// unroll-hint-value: |
| 2028 | /// constant-expression |
| 2029 | /// |
| 2030 | /// Loop unrolling hints can be specified with '#pragma unroll' or |
| 2031 | /// '#pragma nounroll'. '#pragma unroll' can take a numeric argument optionally |
| 2032 | /// contained in parentheses. With no argument the directive instructs llvm to |
| 2033 | /// try to unroll the loop completely. A positive integer argument can be |
| 2034 | /// specified to indicate the number of times the loop should be unrolled. To |
| 2035 | /// maximize compatibility with other compilers the unroll count argument can be |
| 2036 | /// specified with or without parentheses. Specifying, '#pragma nounroll' |
| 2037 | /// disables unrolling of the loop. |
| 2038 | void PragmaUnrollHintHandler::HandlePragma(Preprocessor &PP, |
| 2039 | PragmaIntroducerKind Introducer, |
| 2040 | Token &Tok) { |
| 2041 | // Incoming token is "unroll" for "#pragma unroll", or "nounroll" for |
| 2042 | // "#pragma nounroll". |
| 2043 | Token PragmaName = Tok; |
| 2044 | PP.Lex(Tok); |
| 2045 | auto *Info = new (PP.getPreprocessorAllocator()) PragmaLoopHintInfo; |
| 2046 | if (Tok.is(tok::eod)) { |
| 2047 | // nounroll or unroll pragma without an argument. |
| 2048 | Info->PragmaName = PragmaName; |
| 2049 | Info->Option.startToken(); |
| 2050 | } else if (PragmaName.getIdentifierInfo()->getName() == "nounroll") { |
| 2051 | PP.Diag(Tok.getLocation(), diag::warn_pragma_extra_tokens_at_eol) |
| 2052 | << "nounroll"; |
| 2053 | return; |
| 2054 | } else { |
| 2055 | // Unroll pragma with an argument: "#pragma unroll N" or |
| 2056 | // "#pragma unroll(N)". |
| 2057 | // Read '(' if it exists. |
| 2058 | bool ValueInParens = Tok.is(tok::l_paren); |
| 2059 | if (ValueInParens) |
| 2060 | PP.Lex(Tok); |
| 2061 | |
| 2062 | Token Option; |
| 2063 | Option.startToken(); |
| 2064 | if (ParseLoopHintValue(PP, Tok, PragmaName, Option, ValueInParens, *Info)) |
| 2065 | return; |
| 2066 | |
| 2067 | // In CUDA, the argument to '#pragma unroll' should not be contained in |
| 2068 | // parentheses. |
| 2069 | if (PP.getLangOpts().CUDA && ValueInParens) |
| 2070 | PP.Diag(Info->Toks[0].getLocation(), |
| 2071 | diag::warn_pragma_unroll_cuda_value_in_parens); |
| 2072 | |
| 2073 | if (Tok.isNot(tok::eod)) { |
| 2074 | PP.Diag(Tok.getLocation(), diag::warn_pragma_extra_tokens_at_eol) |
| 2075 | << "unroll"; |
| 2076 | return; |
| 2077 | } |
| 2078 | } |
| 2079 | |
| 2080 | // Generate the hint token. |
| 2081 | Token *TokenArray = new Token[1]; |
| 2082 | TokenArray[0].startToken(); |
| 2083 | TokenArray[0].setKind(tok::annot_pragma_loop_hint); |
| 2084 | TokenArray[0].setLocation(PragmaName.getLocation()); |
| 2085 | TokenArray[0].setAnnotationValue(static_cast<void *>(Info)); |
| 2086 | PP.EnterTokenStream(TokenArray, 1, /*DisableMacroExpansion=*/false, |
| 2087 | /*OwnsTokens=*/true); |
| 2088 | } |