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