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