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