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