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