Coverage Report

Created: 2019-07-24 05:18

/Users/buildslave/jenkins/workspace/clang-stage2-coverage-R/llvm/tools/clang/lib/Lex/Pragma.cpp
Line
Count
Source (jump to first uncovered line)
1
//===- Pragma.cpp - Pragma registration and handling ----------------------===//
2
//
3
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4
// See https://llvm.org/LICENSE.txt for license information.
5
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6
//
7
//===----------------------------------------------------------------------===//
8
//
9
// This file implements the PragmaHandler/PragmaTable interfaces and implements
10
// pragma related methods of the Preprocessor class.
11
//
12
//===----------------------------------------------------------------------===//
13
14
#include "clang/Lex/Pragma.h"
15
#include "clang/Basic/Diagnostic.h"
16
#include "clang/Basic/FileManager.h"
17
#include "clang/Basic/IdentifierTable.h"
18
#include "clang/Basic/LLVM.h"
19
#include "clang/Basic/LangOptions.h"
20
#include "clang/Basic/Module.h"
21
#include "clang/Basic/SourceLocation.h"
22
#include "clang/Basic/SourceManager.h"
23
#include "clang/Basic/TokenKinds.h"
24
#include "clang/Lex/HeaderSearch.h"
25
#include "clang/Lex/LexDiagnostic.h"
26
#include "clang/Lex/Lexer.h"
27
#include "clang/Lex/LiteralSupport.h"
28
#include "clang/Lex/MacroInfo.h"
29
#include "clang/Lex/ModuleLoader.h"
30
#include "clang/Lex/PPCallbacks.h"
31
#include "clang/Lex/Preprocessor.h"
32
#include "clang/Lex/PreprocessorLexer.h"
33
#include "clang/Lex/Token.h"
34
#include "clang/Lex/TokenLexer.h"
35
#include "llvm/ADT/ArrayRef.h"
36
#include "llvm/ADT/DenseMap.h"
37
#include "llvm/ADT/STLExtras.h"
38
#include "llvm/ADT/SmallString.h"
39
#include "llvm/ADT/SmallVector.h"
40
#include "llvm/ADT/StringSwitch.h"
41
#include "llvm/ADT/StringRef.h"
42
#include "llvm/Support/CrashRecoveryContext.h"
43
#include "llvm/Support/Compiler.h"
44
#include "llvm/Support/ErrorHandling.h"
45
#include <algorithm>
46
#include <cassert>
47
#include <cstddef>
48
#include <cstdint>
49
#include <limits>
50
#include <string>
51
#include <utility>
52
#include <vector>
53
54
using namespace clang;
55
56
// Out-of-line destructor to provide a home for the class.
57
1.99M
PragmaHandler::~PragmaHandler() = default;
58
59
//===----------------------------------------------------------------------===//
60
// EmptyPragmaHandler Implementation.
61
//===----------------------------------------------------------------------===//
62
63
6.23k
EmptyPragmaHandler::EmptyPragmaHandler(StringRef Name) : PragmaHandler(Name) {}
64
65
void EmptyPragmaHandler::HandlePragma(Preprocessor &PP,
66
                                      PragmaIntroducer Introducer,
67
16
                                      Token &FirstToken) {}
68
69
//===----------------------------------------------------------------------===//
70
// PragmaNamespace Implementation.
71
//===----------------------------------------------------------------------===//
72
73
179k
PragmaNamespace::~PragmaNamespace() {
74
179k
  llvm::DeleteContainerSeconds(Handlers);
75
179k
}
76
77
/// FindHandler - Check to see if there is already a handler for the
78
/// specified name.  If not, return the handler for the null identifier if it
79
/// exists, otherwise return null.  If IgnoreNull is true (the default) then
80
/// the null handler isn't returned on failure to match.
81
PragmaHandler *PragmaNamespace::FindHandler(StringRef Name,
82
1.77M
                                            bool IgnoreNull) const {
83
1.77M
  if (PragmaHandler *Handler = Handlers.lookup(Name))
84
1.63M
    return Handler;
85
132k
  return IgnoreNull ? 
nullptr131k
:
Handlers.lookup(StringRef())1.13k
;
86
132k
}
87
88
2.25M
void PragmaNamespace::AddPragma(PragmaHandler *Handler) {
89
2.25M
  assert(!Handlers.lookup(Handler->getName()) &&
90
2.25M
         "A handler with this name is already registered in this namespace");
91
2.25M
  Handlers[Handler->getName()] = Handler;
92
2.25M
}
93
94
1.00M
void PragmaNamespace::RemovePragmaHandler(PragmaHandler *Handler) {
95
1.00M
  assert(Handlers.lookup(Handler->getName()) &&
96
1.00M
         "Handler not registered in this namespace");
97
1.00M
  Handlers.erase(Handler->getName());
98
1.00M
}
99
100
void PragmaNamespace::HandlePragma(Preprocessor &PP,
101
310k
                                   PragmaIntroducer Introducer, Token &Tok) {
102
310k
  // Read the 'namespace' that the directive is in, e.g. STDC.  Do not macro
103
310k
  // expand it, the user can have a STDC #define, that should not affect this.
104
310k
  PP.LexUnexpandedToken(Tok);
105
310k
106
310k
  // Get the handler for this token.  If there is no handler, ignore the pragma.
107
310k
  PragmaHandler *Handler
108
310k
    = FindHandler(Tok.getIdentifierInfo() ? 
Tok.getIdentifierInfo()->getName()310k
109
310k
                                          : 
StringRef()8
,
110
310k
                  /*IgnoreNull=*/false);
111
310k
  if (!Handler) {
112
1.04k
    PP.Diag(Tok, diag::warn_pragma_ignored);
113
1.04k
    return;
114
1.04k
  }
115
309k
116
309k
  // Otherwise, pass it down.
117
309k
  Handler->HandlePragma(PP, Introducer, Tok);
118
309k
}
119
120
//===----------------------------------------------------------------------===//
121
// Preprocessor Pragma Directive Handling.
122
//===----------------------------------------------------------------------===//
123
124
/// HandlePragmaDirective - The "\#pragma" directive has been parsed.  Lex the
125
/// rest of the pragma, passing it to the registered pragma handlers.
126
265k
void Preprocessor::HandlePragmaDirective(PragmaIntroducer Introducer) {
127
265k
  if (Callbacks)
128
265k
    Callbacks->PragmaDirective(Introducer.Loc, Introducer.Kind);
129
265k
130
265k
  if (!PragmasEnabled)
131
3
    return;
132
265k
133
265k
  ++NumPragma;
134
265k
135
265k
  // Invoke the first level of pragma handlers which reads the namespace id.
136
265k
  Token Tok;
137
265k
  PragmaHandlers->HandlePragma(*this, Introducer, Tok);
138
265k
139
265k
  // If the pragma handler didn't read the rest of the line, consume it now.
140
265k
  if ((CurTokenLexer && 
CurTokenLexer->isParsingPreprocessorDirective()197k
)
141
265k
   || 
(265k
CurPPLexer265k
&&
CurPPLexer->ParsingPreprocessorDirective67.9k
))
142
26.5k
    DiscardUntilEndOfDirective();
143
265k
}
144
145
/// Handle_Pragma - Read a _Pragma directive, slice it up, process it, then
146
/// return the first token after the directive.  The _Pragma token has just
147
/// been read into 'Tok'.
148
36.4k
void Preprocessor::Handle_Pragma(Token &Tok) {
149
36.4k
  // C11 6.10.3.4/3:
150
36.4k
  //   all pragma unary operator expressions within [a completely
151
36.4k
  //   macro-replaced preprocessing token sequence] are [...] processed [after
152
36.4k
  //   rescanning is complete]
153
36.4k
  //
154
36.4k
  // This means that we execute _Pragma operators in two cases:
155
36.4k
  //
156
36.4k
  //  1) on token sequences that would otherwise be produced as the output of
157
36.4k
  //     phase 4 of preprocessing, and
158
36.4k
  //  2) on token sequences formed as the macro-replaced token sequence of a
159
36.4k
  //     macro argument
160
36.4k
  //
161
36.4k
  // Case #2 appears to be a wording bug: only _Pragmas that would survive to
162
36.4k
  // the end of phase 4 should actually be executed. Discussion on the WG14
163
36.4k
  // mailing list suggests that a _Pragma operator is notionally checked early,
164
36.4k
  // but only pragmas that survive to the end of phase 4 should be executed.
165
36.4k
  //
166
36.4k
  // In Case #2, we check the syntax now, but then put the tokens back into the
167
36.4k
  // token stream for later consumption.
168
36.4k
169
36.4k
  struct TokenCollector {
170
36.4k
    Preprocessor &Self;
171
36.4k
    bool Collect;
172
36.4k
    SmallVector<Token, 3> Tokens;
173
36.4k
    Token &Tok;
174
36.4k
175
109k
    void lex() {
176
109k
      if (Collect)
177
51
        Tokens.push_back(Tok);
178
109k
      Self.Lex(Tok);
179
109k
    }
180
36.4k
181
36.4k
    void revert() {
182
16
      assert(Collect && "did not collect tokens");
183
16
      assert(!Tokens.empty() && "collected unexpected number of tokens");
184
16
185
16
      // Push the ( "string" ) tokens into the token stream.
186
16
      auto Toks = llvm::make_unique<Token[]>(Tokens.size());
187
16
      std::copy(Tokens.begin() + 1, Tokens.end(), Toks.get());
188
16
      Toks[Tokens.size() - 1] = Tok;
189
16
      Self.EnterTokenStream(std::move(Toks), Tokens.size(),
190
16
                            /*DisableMacroExpansion*/ true,
191
16
                            /*IsReinject*/ true);
192
16
193
16
      // ... and return the _Pragma token unchanged.
194
16
      Tok = *Tokens.begin();
195
16
    }
196
36.4k
  };
197
36.4k
198
36.4k
  TokenCollector Toks = {*this, InMacroArgPreExpansion, {}, Tok};
199
36.4k
200
36.4k
  // Remember the pragma token location.
201
36.4k
  SourceLocation PragmaLoc = Tok.getLocation();
202
36.4k
203
36.4k
  // Read the '('.
204
36.4k
  Toks.lex();
205
36.4k
  if (Tok.isNot(tok::l_paren)) {
206
1
    Diag(PragmaLoc, diag::err__Pragma_malformed);
207
1
    return;
208
1
  }
209
36.4k
210
36.4k
  // Read the '"..."'.
211
36.4k
  Toks.lex();
212
36.4k
  if (!tok::isStringLiteral(Tok.getKind())) {
213
4
    Diag(PragmaLoc, diag::err__Pragma_malformed);
214
4
    // Skip bad tokens, and the ')', if present.
215
4
    if (Tok.isNot(tok::r_paren) && Tok.isNot(tok::eof))
216
3
      Lex(Tok);
217
7
    while (Tok.isNot(tok::r_paren) &&
218
7
           
!Tok.isAtStartOfLine()5
&&
219
7
           
Tok.isNot(tok::eof)5
)
220
3
      Lex(Tok);
221
4
    if (Tok.is(tok::r_paren))
222
2
      Lex(Tok);
223
4
    return;
224
4
  }
225
36.4k
226
36.4k
  if (Tok.hasUDSuffix()) {
227
1
    Diag(Tok, diag::err_invalid_string_udl);
228
1
    // Skip this token, and the ')', if present.
229
1
    Lex(Tok);
230
1
    if (Tok.is(tok::r_paren))
231
1
      Lex(Tok);
232
1
    return;
233
1
  }
234
36.4k
235
36.4k
  // Remember the string.
236
36.4k
  Token StrTok = Tok;
237
36.4k
238
36.4k
  // Read the ')'.
239
36.4k
  Toks.lex();
240
36.4k
  if (Tok.isNot(tok::r_paren)) {
241
0
    Diag(PragmaLoc, diag::err__Pragma_malformed);
242
0
    return;
243
0
  }
244
36.4k
245
36.4k
  // If we're expanding a macro argument, put the tokens back.
246
36.4k
  if (InMacroArgPreExpansion) {
247
16
    Toks.revert();
248
16
    return;
249
16
  }
250
36.4k
251
36.4k
  SourceLocation RParenLoc = Tok.getLocation();
252
36.4k
  std::string StrVal = getSpelling(StrTok);
253
36.4k
254
36.4k
  // The _Pragma is lexically sound.  Destringize according to C11 6.10.9.1:
255
36.4k
  // "The string literal is destringized by deleting any encoding prefix,
256
36.4k
  // deleting the leading and trailing double-quotes, replacing each escape
257
36.4k
  // sequence \" by a double-quote, and replacing each escape sequence \\ by a
258
36.4k
  // single backslash."
259
36.4k
  if (StrVal[0] == 'L' || 
StrVal[0] == 'U'36.4k
||
260
36.4k
      
(36.4k
StrVal[0] == 'u'36.4k
&&
StrVal[1] != '8'3
))
261
4
    StrVal.erase(StrVal.begin());
262
36.4k
  else if (StrVal[0] == 'u')
263
2
    StrVal.erase(StrVal.begin(), StrVal.begin() + 2);
264
36.4k
265
36.4k
  if (StrVal[0] == 'R') {
266
3
    // FIXME: C++11 does not specify how to handle raw-string-literals here.
267
3
    // We strip off the 'R', the quotes, the d-char-sequences, and the parens.
268
3
    assert(StrVal[1] == '"' && StrVal[StrVal.size() - 1] == '"' &&
269
3
           "Invalid raw string token!");
270
3
271
3
    // Measure the length of the d-char-sequence.
272
3
    unsigned NumDChars = 0;
273
4
    while (StrVal[2 + NumDChars] != '(') {
274
1
      assert(NumDChars < (StrVal.size() - 5) / 2 &&
275
1
             "Invalid raw string token!");
276
1
      ++NumDChars;
277
1
    }
278
3
    assert(StrVal[StrVal.size() - 2 - NumDChars] == ')');
279
3
280
3
    // Remove 'R " d-char-sequence' and 'd-char-sequence "'. We'll replace the
281
3
    // parens below.
282
3
    StrVal.erase(0, 2 + NumDChars);
283
3
    StrVal.erase(StrVal.size() - 1 - NumDChars);
284
36.4k
  } else {
285
36.4k
    assert(StrVal[0] == '"' && StrVal[StrVal.size()-1] == '"' &&
286
36.4k
           "Invalid string token!");
287
36.4k
288
36.4k
    // Remove escaped quotes and escapes.
289
36.4k
    unsigned ResultPos = 1;
290
788k
    for (size_t i = 1, e = StrVal.size() - 1; i != e; 
++i751k
) {
291
751k
      // Skip escapes.  \\ -> '\' and \" -> '"'.
292
751k
      if (StrVal[i] == '\\' && 
i + 1 < e49.7k
&&
293
751k
          
(49.7k
StrVal[i + 1] == '\\'49.7k
||
StrVal[i + 1] == '"'49.7k
))
294
49.7k
        ++i;
295
751k
      StrVal[ResultPos++] = StrVal[i];
296
751k
    }
297
36.4k
    StrVal.erase(StrVal.begin() + ResultPos, StrVal.end() - 1);
298
36.4k
  }
299
36.4k
300
36.4k
  // Remove the front quote, replacing it with a space, so that the pragma
301
36.4k
  // contents appear to have a space before them.
302
36.4k
  StrVal[0] = ' ';
303
36.4k
304
36.4k
  // Replace the terminating quote with a \n.
305
36.4k
  StrVal[StrVal.size()-1] = '\n';
306
36.4k
307
36.4k
  // Plop the string (including the newline and trailing null) into a buffer
308
36.4k
  // where we can lex it.
309
36.4k
  Token TmpTok;
310
36.4k
  TmpTok.startToken();
311
36.4k
  CreateString(StrVal, TmpTok);
312
36.4k
  SourceLocation TokLoc = TmpTok.getLocation();
313
36.4k
314
36.4k
  // Make and enter a lexer object so that we lex and expand the tokens just
315
36.4k
  // like any others.
316
36.4k
  Lexer *TL = Lexer::Create_PragmaLexer(TokLoc, PragmaLoc, RParenLoc,
317
36.4k
                                        StrVal.size(), *this);
318
36.4k
319
36.4k
  EnterSourceFileWithLexer(TL, nullptr);
320
36.4k
321
36.4k
  // With everything set up, lex this as a #pragma directive.
322
36.4k
  HandlePragmaDirective({PIK__Pragma, PragmaLoc});
323
36.4k
324
36.4k
  // Finally, return whatever came after the pragma directive.
325
36.4k
  return Lex(Tok);
326
36.4k
}
327
328
/// HandleMicrosoft__pragma - Like Handle_Pragma except the pragma text
329
/// is not enclosed within a string literal.
330
19
void Preprocessor::HandleMicrosoft__pragma(Token &Tok) {
331
19
  // Remember the pragma token location.
332
19
  SourceLocation PragmaLoc = Tok.getLocation();
333
19
334
19
  // Read the '('.
335
19
  Lex(Tok);
336
19
  if (Tok.isNot(tok::l_paren)) {
337
0
    Diag(PragmaLoc, diag::err__Pragma_malformed);
338
0
    return;
339
0
  }
340
19
341
19
  // Get the tokens enclosed within the __pragma(), as well as the final ')'.
342
19
  SmallVector<Token, 32> PragmaToks;
343
19
  int NumParens = 0;
344
19
  Lex(Tok);
345
105
  while (Tok.isNot(tok::eof)) {
346
105
    PragmaToks.push_back(Tok);
347
105
    if (Tok.is(tok::l_paren))
348
16
      NumParens++;
349
89
    else if (Tok.is(tok::r_paren) && 
NumParens-- == 035
)
350
19
      break;
351
86
    Lex(Tok);
352
86
  }
353
19
354
19
  if (Tok.is(tok::eof)) {
355
0
    Diag(PragmaLoc, diag::err_unterminated___pragma);
356
0
    return;
357
0
  }
358
19
359
19
  PragmaToks.front().setFlag(Token::LeadingSpace);
360
19
361
19
  // Replace the ')' with an EOD to mark the end of the pragma.
362
19
  PragmaToks.back().setKind(tok::eod);
363
19
364
19
  Token *TokArray = new Token[PragmaToks.size()];
365
19
  std::copy(PragmaToks.begin(), PragmaToks.end(), TokArray);
366
19
367
19
  // Push the tokens onto the stack.
368
19
  EnterTokenStream(TokArray, PragmaToks.size(), true, true,
369
19
                   /*IsReinject*/ false);
370
19
371
19
  // With everything set up, lex this as a #pragma directive.
372
19
  HandlePragmaDirective({PIK___pragma, PragmaLoc});
373
19
374
19
  // Finally, return whatever came after the pragma directive.
375
19
  return Lex(Tok);
376
19
}
377
378
/// HandlePragmaOnce - Handle \#pragma once.  OnceTok is the 'once'.
379
116
void Preprocessor::HandlePragmaOnce(Token &OnceTok) {
380
116
  // Don't honor the 'once' when handling the primary source file, unless
381
116
  // this is a prefix to a TU, which indicates we're generating a PCH file, or
382
116
  // when the main file is a header (e.g. when -xc-header is provided on the
383
116
  // commandline).
384
116
  if (isInPrimaryFile() && 
TUKind != TU_Prefix6
&&
!getLangOpts().IsHeaderFile4
) {
385
3
    Diag(OnceTok, diag::pp_pragma_once_in_main_file);
386
3
    return;
387
3
  }
388
113
389
113
  // Get the current file lexer we're looking at.  Ignore _Pragma 'files' etc.
390
113
  // Mark the file as a once-only file now.
391
113
  HeaderInfo.MarkFileIncludeOnce(getCurrentFileLexer()->getFileEntry());
392
113
}
393
394
1.54k
void Preprocessor::HandlePragmaMark() {
395
1.54k
  assert(CurPPLexer && "No current lexer?");
396
1.54k
  CurLexer->ReadToEndOfLine();
397
1.54k
}
398
399
/// HandlePragmaPoison - Handle \#pragma GCC poison.  PoisonTok is the 'poison'.
400
5
void Preprocessor::HandlePragmaPoison() {
401
5
  Token Tok;
402
5
403
10
  while (true) {
404
10
    // Read the next token to poison.  While doing this, pretend that we are
405
10
    // skipping while reading the identifier to poison.
406
10
    // This avoids errors on code like:
407
10
    //   #pragma GCC poison X
408
10
    //   #pragma GCC poison X
409
10
    if (CurPPLexer) CurPPLexer->LexingRawMode = true;
410
10
    LexUnexpandedToken(Tok);
411
10
    if (CurPPLexer) CurPPLexer->LexingRawMode = false;
412
10
413
10
    // If we reached the end of line, we're done.
414
10
    if (Tok.is(tok::eod)) 
return5
;
415
5
416
5
    // Can only poison identifiers.
417
5
    if (Tok.isNot(tok::raw_identifier)) {
418
0
      Diag(Tok, diag::err_pp_invalid_poison);
419
0
      return;
420
0
    }
421
5
422
5
    // Look up the identifier info for the token.  We disabled identifier lookup
423
5
    // by saying we're skipping contents, so we need to do this manually.
424
5
    IdentifierInfo *II = LookUpIdentifierInfo(Tok);
425
5
426
5
    // Already poisoned.
427
5
    if (II->isPoisoned()) 
continue1
;
428
4
429
4
    // If this is a macro identifier, emit a warning.
430
4
    if (isMacroDefined(II))
431
0
      Diag(Tok, diag::pp_poisoning_existing_macro);
432
4
433
4
    // Finally, poison it!
434
4
    II->setIsPoisoned();
435
4
    if (II->isFromAST())
436
0
      II->setChangedSinceDeserialization();
437
4
  }
438
5
}
439
440
/// HandlePragmaSystemHeader - Implement \#pragma GCC system_header.  We know
441
/// that the whole directive has been parsed.
442
28.2k
void Preprocessor::HandlePragmaSystemHeader(Token &SysHeaderTok) {
443
28.2k
  if (isInPrimaryFile()) {
444
1
    Diag(SysHeaderTok, diag::pp_pragma_sysheader_in_main_file);
445
1
    return;
446
1
  }
447
28.2k
448
28.2k
  // Get the current file lexer we're looking at.  Ignore _Pragma 'files' etc.
449
28.2k
  PreprocessorLexer *TheLexer = getCurrentFileLexer();
450
28.2k
451
28.2k
  // Mark the file as a system header.
452
28.2k
  HeaderInfo.MarkFileSystemHeader(TheLexer->getFileEntry());
453
28.2k
454
28.2k
  PresumedLoc PLoc = SourceMgr.getPresumedLoc(SysHeaderTok.getLocation());
455
28.2k
  if (PLoc.isInvalid())
456
0
    return;
457
28.2k
458
28.2k
  unsigned FilenameID = SourceMgr.getLineTableFilenameID(PLoc.getFilename());
459
28.2k
460
28.2k
  // Notify the client, if desired, that we are in a new source file.
461
28.2k
  if (Callbacks)
462
28.2k
    Callbacks->FileChanged(SysHeaderTok.getLocation(),
463
28.2k
                           PPCallbacks::SystemHeaderPragma, SrcMgr::C_System);
464
28.2k
465
28.2k
  // Emit a line marker.  This will change any source locations from this point
466
28.2k
  // forward to realize they are in a system header.
467
28.2k
  // Create a line note with this information.
468
28.2k
  SourceMgr.AddLineNote(SysHeaderTok.getLocation(), PLoc.getLine() + 1,
469
28.2k
                        FilenameID, /*IsEntry=*/false, /*IsExit=*/false,
470
28.2k
                        SrcMgr::C_System);
471
28.2k
}
472
473
/// HandlePragmaDependency - Handle \#pragma GCC dependency "foo" blah.
474
4
void Preprocessor::HandlePragmaDependency(Token &DependencyTok) {
475
4
  Token FilenameTok;
476
4
  if (LexHeaderName(FilenameTok, /*AllowConcatenation*/false))
477
0
    return;
478
4
479
4
  // If the next token wasn't a header-name, diagnose the error.
480
4
  if (FilenameTok.isNot(tok::header_name)) {
481
1
    Diag(FilenameTok.getLocation(), diag::err_pp_expects_filename);
482
1
    return;
483
1
  }
484
3
485
3
  // Reserve a buffer to get the spelling.
486
3
  SmallString<128> FilenameBuffer;
487
3
  bool Invalid = false;
488
3
  StringRef Filename = getSpelling(FilenameTok, FilenameBuffer, &Invalid);
489
3
  if (Invalid)
490
0
    return;
491
3
492
3
  bool isAngled =
493
3
    GetIncludeFilenameSpelling(FilenameTok.getLocation(), Filename);
494
3
  // If GetIncludeFilenameSpelling set the start ptr to null, there was an
495
3
  // error.
496
3
  if (Filename.empty())
497
0
    return;
498
3
499
3
  // Search include directories for this file.
500
3
  const DirectoryLookup *CurDir;
501
3
  const FileEntry *File =
502
3
      LookupFile(FilenameTok.getLocation(), Filename, isAngled, nullptr,
503
3
                 nullptr, CurDir, nullptr, nullptr, nullptr, nullptr, nullptr);
504
3
  if (!File) {
505
2
    if (!SuppressIncludeNotFoundError)
506
2
      Diag(FilenameTok, diag::err_pp_file_not_found) << Filename;
507
2
    return;
508
2
  }
509
1
510
1
  const FileEntry *CurFile = getCurrentFileLexer()->getFileEntry();
511
1
512
1
  // If this file is older than the file it depends on, emit a diagnostic.
513
1
  if (CurFile && CurFile->getModificationTime() < File->getModificationTime()) {
514
0
    // Lex tokens at the end of the message and include them in the message.
515
0
    std::string Message;
516
0
    Lex(DependencyTok);
517
0
    while (DependencyTok.isNot(tok::eod)) {
518
0
      Message += getSpelling(DependencyTok) + " ";
519
0
      Lex(DependencyTok);
520
0
    }
521
0
522
0
    // Remove the trailing ' ' if present.
523
0
    if (!Message.empty())
524
0
      Message.erase(Message.end()-1);
525
0
    Diag(FilenameTok, diag::pp_out_of_date_dependency) << Message;
526
0
  }
527
1
}
528
529
/// ParsePragmaPushOrPopMacro - Handle parsing of pragma push_macro/pop_macro.
530
/// Return the IdentifierInfo* associated with the macro to push or pop.
531
24.7k
IdentifierInfo *Preprocessor::ParsePragmaPushOrPopMacro(Token &Tok) {
532
24.7k
  // Remember the pragma token location.
533
24.7k
  Token PragmaTok = Tok;
534
24.7k
535
24.7k
  // Read the '('.
536
24.7k
  Lex(Tok);
537
24.7k
  if (Tok.isNot(tok::l_paren)) {
538
0
    Diag(PragmaTok.getLocation(), diag::err_pragma_push_pop_macro_malformed)
539
0
      << getSpelling(PragmaTok);
540
0
    return nullptr;
541
0
  }
542
24.7k
543
24.7k
  // Read the macro name string.
544
24.7k
  Lex(Tok);
545
24.7k
  if (Tok.isNot(tok::string_literal)) {
546
0
    Diag(PragmaTok.getLocation(), diag::err_pragma_push_pop_macro_malformed)
547
0
      << getSpelling(PragmaTok);
548
0
    return nullptr;
549
0
  }
550
24.7k
551
24.7k
  if (Tok.hasUDSuffix()) {
552
1
    Diag(Tok, diag::err_invalid_string_udl);
553
1
    return nullptr;
554
1
  }
555
24.7k
556
24.7k
  // Remember the macro string.
557
24.7k
  std::string StrVal = getSpelling(Tok);
558
24.7k
559
24.7k
  // Read the ')'.
560
24.7k
  Lex(Tok);
561
24.7k
  if (Tok.isNot(tok::r_paren)) {
562
0
    Diag(PragmaTok.getLocation(), diag::err_pragma_push_pop_macro_malformed)
563
0
      << getSpelling(PragmaTok);
564
0
    return nullptr;
565
0
  }
566
24.7k
567
24.7k
  assert(StrVal[0] == '"' && StrVal[StrVal.size()-1] == '"' &&
568
24.7k
         "Invalid string token!");
569
24.7k
570
24.7k
  // Create a Token from the string.
571
24.7k
  Token MacroTok;
572
24.7k
  MacroTok.startToken();
573
24.7k
  MacroTok.setKind(tok::raw_identifier);
574
24.7k
  CreateString(StringRef(&StrVal[1], StrVal.size() - 2), MacroTok);
575
24.7k
576
24.7k
  // Get the IdentifierInfo of MacroToPushTok.
577
24.7k
  return LookUpIdentifierInfo(MacroTok);
578
24.7k
}
579
580
/// Handle \#pragma push_macro.
581
///
582
/// The syntax is:
583
/// \code
584
///   #pragma push_macro("macro")
585
/// \endcode
586
12.3k
void Preprocessor::HandlePragmaPushMacro(Token &PushMacroTok) {
587
12.3k
  // Parse the pragma directive and get the macro IdentifierInfo*.
588
12.3k
  IdentifierInfo *IdentInfo = ParsePragmaPushOrPopMacro(PushMacroTok);
589
12.3k
  if (!IdentInfo) 
return1
;
590
12.3k
591
12.3k
  // Get the MacroInfo associated with IdentInfo.
592
12.3k
  MacroInfo *MI = getMacroInfo(IdentInfo);
593
12.3k
594
12.3k
  if (MI) {
595
9
    // Allow the original MacroInfo to be redefined later.
596
9
    MI->setIsAllowRedefinitionsWithoutWarning(true);
597
9
  }
598
12.3k
599
12.3k
  // Push the cloned MacroInfo so we can retrieve it later.
600
12.3k
  PragmaPushMacroInfo[IdentInfo].push_back(MI);
601
12.3k
}
602
603
/// Handle \#pragma pop_macro.
604
///
605
/// The syntax is:
606
/// \code
607
///   #pragma pop_macro("macro")
608
/// \endcode
609
12.3k
void Preprocessor::HandlePragmaPopMacro(Token &PopMacroTok) {
610
12.3k
  SourceLocation MessageLoc = PopMacroTok.getLocation();
611
12.3k
612
12.3k
  // Parse the pragma directive and get the macro IdentifierInfo*.
613
12.3k
  IdentifierInfo *IdentInfo = ParsePragmaPushOrPopMacro(PopMacroTok);
614
12.3k
  if (!IdentInfo) 
return0
;
615
12.3k
616
12.3k
  // Find the vector<MacroInfo*> associated with the macro.
617
12.3k
  llvm::DenseMap<IdentifierInfo *, std::vector<MacroInfo *>>::iterator iter =
618
12.3k
    PragmaPushMacroInfo.find(IdentInfo);
619
12.3k
  if (iter != PragmaPushMacroInfo.end()) {
620
12.3k
    // Forget the MacroInfo currently associated with IdentInfo.
621
12.3k
    if (MacroInfo *MI = getMacroInfo(IdentInfo)) {
622
19
      if (MI->isWarnIfUnused())
623
0
        WarnUnusedMacroLocs.erase(MI->getDefinitionLoc());
624
19
      appendMacroDirective(IdentInfo, AllocateUndefMacroDirective(MessageLoc));
625
19
    }
626
12.3k
627
12.3k
    // Get the MacroInfo we want to reinstall.
628
12.3k
    MacroInfo *MacroToReInstall = iter->second.back();
629
12.3k
630
12.3k
    if (MacroToReInstall)
631
8
      // Reinstall the previously pushed macro.
632
8
      appendDefMacroDirective(IdentInfo, MacroToReInstall, MessageLoc);
633
12.3k
634
12.3k
    // Pop PragmaPushMacroInfo stack.
635
12.3k
    iter->second.pop_back();
636
12.3k
    if (iter->second.empty())
637
12.3k
      PragmaPushMacroInfo.erase(iter);
638
12.3k
  } else {
639
0
    Diag(MessageLoc, diag::warn_pragma_pop_macro_no_push)
640
0
      << IdentInfo->getName();
641
0
  }
642
12.3k
}
643
644
22
void Preprocessor::HandlePragmaIncludeAlias(Token &Tok) {
645
22
  // We will either get a quoted filename or a bracketed filename, and we
646
22
  // have to track which we got.  The first filename is the source name,
647
22
  // and the second name is the mapped filename.  If the first is quoted,
648
22
  // the second must be as well (cannot mix and match quotes and brackets).
649
22
650
22
  // Get the open paren
651
22
  Lex(Tok);
652
22
  if (Tok.isNot(tok::l_paren)) {
653
0
    Diag(Tok, diag::warn_pragma_include_alias_expected) << "(";
654
0
    return;
655
0
  }
656
22
657
22
  // We expect either a quoted string literal, or a bracketed name
658
22
  Token SourceFilenameTok;
659
22
  if (LexHeaderName(SourceFilenameTok))
660
0
    return;
661
22
662
22
  StringRef SourceFileName;
663
22
  SmallString<128> FileNameBuffer;
664
22
  if (SourceFilenameTok.is(tok::header_name)) {
665
20
    SourceFileName = getSpelling(SourceFilenameTok, FileNameBuffer);
666
20
  } else {
667
2
    Diag(Tok, diag::warn_pragma_include_alias_expected_filename);
668
2
    return;
669
2
  }
670
20
  FileNameBuffer.clear();
671
20
672
20
  // Now we expect a comma, followed by another include name
673
20
  Lex(Tok);
674
20
  if (Tok.isNot(tok::comma)) {
675
2
    Diag(Tok, diag::warn_pragma_include_alias_expected) << ",";
676
2
    return;
677
2
  }
678
18
679
18
  Token ReplaceFilenameTok;
680
18
  if (LexHeaderName(ReplaceFilenameTok))
681
0
    return;
682
18
683
18
  StringRef ReplaceFileName;
684
18
  if (ReplaceFilenameTok.is(tok::header_name)) {
685
18
    ReplaceFileName = getSpelling(ReplaceFilenameTok, FileNameBuffer);
686
18
  } else {
687
0
    Diag(Tok, diag::warn_pragma_include_alias_expected_filename);
688
0
    return;
689
0
  }
690
18
691
18
  // Finally, we expect the closing paren
692
18
  Lex(Tok);
693
18
  if (Tok.isNot(tok::r_paren)) {
694
0
    Diag(Tok, diag::warn_pragma_include_alias_expected) << ")";
695
0
    return;
696
0
  }
697
18
698
18
  // Now that we have the source and target filenames, we need to make sure
699
18
  // they're both of the same type (angled vs non-angled)
700
18
  StringRef OriginalSource = SourceFileName;
701
18
702
18
  bool SourceIsAngled =
703
18
    GetIncludeFilenameSpelling(SourceFilenameTok.getLocation(),
704
18
                                SourceFileName);
705
18
  bool ReplaceIsAngled =
706
18
    GetIncludeFilenameSpelling(ReplaceFilenameTok.getLocation(),
707
18
                                ReplaceFileName);
708
18
  if (!SourceFileName.empty() && 
!ReplaceFileName.empty()16
&&
709
18
      
(SourceIsAngled != ReplaceIsAngled)14
) {
710
4
    unsigned int DiagID;
711
4
    if (SourceIsAngled)
712
2
      DiagID = diag::warn_pragma_include_alias_mismatch_angle;
713
2
    else
714
2
      DiagID = diag::warn_pragma_include_alias_mismatch_quote;
715
4
716
4
    Diag(SourceFilenameTok.getLocation(), DiagID)
717
4
      << SourceFileName
718
4
      << ReplaceFileName;
719
4
720
4
    return;
721
4
  }
722
14
723
14
  // Now we can let the include handler know about this mapping
724
14
  getHeaderSearchInfo().AddIncludeAlias(OriginalSource, ReplaceFileName);
725
14
}
726
727
// Lex a component of a module name: either an identifier or a string literal;
728
// for components that can be expressed both ways, the two forms are equivalent.
729
static bool LexModuleNameComponent(
730
    Preprocessor &PP, Token &Tok,
731
    std::pair<IdentifierInfo *, SourceLocation> &ModuleNameComponent,
732
431
    bool First) {
733
431
  PP.LexUnexpandedToken(Tok);
734
431
  if (Tok.is(tok::string_literal) && 
!Tok.hasUDSuffix()7
) {
735
7
    StringLiteralParser Literal(Tok, PP);
736
7
    if (Literal.hadError)
737
0
      return true;
738
7
    ModuleNameComponent = std::make_pair(
739
7
        PP.getIdentifierInfo(Literal.GetString()), Tok.getLocation());
740
424
  } else if (!Tok.isAnnotation() && Tok.getIdentifierInfo()) {
741
415
    ModuleNameComponent =
742
415
        std::make_pair(Tok.getIdentifierInfo(), Tok.getLocation());
743
415
  } else {
744
9
    PP.Diag(Tok.getLocation(), diag::err_pp_expected_module_name) << First;
745
9
    return true;
746
9
  }
747
422
  return false;
748
422
}
749
750
static bool LexModuleName(
751
    Preprocessor &PP, Token &Tok,
752
    llvm::SmallVectorImpl<std::pair<IdentifierInfo *, SourceLocation>>
753
268
        &ModuleName) {
754
356
  while (true) {
755
356
    std::pair<IdentifierInfo*, SourceLocation> NameComponent;
756
356
    if (LexModuleNameComponent(PP, Tok, NameComponent, ModuleName.empty()))
757
8
      return true;
758
348
    ModuleName.push_back(NameComponent);
759
348
760
348
    PP.LexUnexpandedToken(Tok);
761
348
    if (Tok.isNot(tok::period))
762
260
      return false;
763
348
  }
764
268
}
765
766
75
void Preprocessor::HandlePragmaModuleBuild(Token &Tok) {
767
75
  SourceLocation Loc = Tok.getLocation();
768
75
769
75
  std::pair<IdentifierInfo *, SourceLocation> ModuleNameLoc;
770
75
  if (LexModuleNameComponent(*this, Tok, ModuleNameLoc, true))
771
1
    return;
772
74
  IdentifierInfo *ModuleName = ModuleNameLoc.first;
773
74
774
74
  LexUnexpandedToken(Tok);
775
74
  if (Tok.isNot(tok::eod)) {
776
0
    Diag(Tok, diag::ext_pp_extra_tokens_at_eol) << "pragma";
777
0
    DiscardUntilEndOfDirective();
778
0
  }
779
74
780
74
  CurLexer->LexingRawMode = true;
781
74
782
1.89k
  auto TryConsumeIdentifier = [&](StringRef Ident) -> bool {
783
1.89k
    if (Tok.getKind() != tok::raw_identifier ||
784
1.89k
        
Tok.getRawIdentifier() != Ident1.81k
)
785
784
      return false;
786
1.11k
    CurLexer->Lex(Tok);
787
1.11k
    return true;
788
1.11k
  };
789
74
790
74
  // Scan forward looking for the end of the module.
791
74
  const char *Start = CurLexer->getBufferLocation();
792
74
  const char *End = nullptr;
793
74
  unsigned NestingLevel = 1;
794
5.92k
  while (true) {
795
5.92k
    End = CurLexer->getBufferLocation();
796
5.92k
    CurLexer->Lex(Tok);
797
5.92k
798
5.92k
    if (Tok.is(tok::eof)) {
799
1
      Diag(Loc, diag::err_pp_module_build_missing_end);
800
1
      break;
801
1
    }
802
5.92k
803
5.92k
    if (Tok.isNot(tok::hash) || 
!Tok.isAtStartOfLine()515
) {
804
5.41k
      // Token was part of module; keep going.
805
5.41k
      continue;
806
5.41k
    }
807
515
808
515
    // We hit something directive-shaped; check to see if this is the end
809
515
    // of the module build.
810
515
    CurLexer->ParsingPreprocessorDirective = true;
811
515
    CurLexer->Lex(Tok);
812
515
    if (TryConsumeIdentifier("pragma") && 
TryConsumeIdentifier("clang")345
&&
813
515
        
TryConsumeIdentifier("module")345
) {
814
345
      if (TryConsumeIdentifier("build"))
815
1
        // #pragma clang module build -> entering a nested module build.
816
1
        ++NestingLevel;
817
344
      else if (TryConsumeIdentifier("endbuild")) {
818
74
        // #pragma clang module endbuild -> leaving a module build.
819
74
        if (--NestingLevel == 0)
820
73
          break;
821
272
      }
822
272
      // We should either be looking at the EOD or more of the current directive
823
272
      // preceding the EOD. Either way we can ignore this token and keep going.
824
272
      assert(Tok.getKind() != tok::eof && "missing EOD before EOF");
825
272
    }
826
515
  }
827
74
828
74
  CurLexer->LexingRawMode = false;
829
74
830
74
  // Load the extracted text as a preprocessed module.
831
74
  assert(CurLexer->getBuffer().begin() <= Start &&
832
74
         Start <= CurLexer->getBuffer().end() &&
833
74
         CurLexer->getBuffer().begin() <= End &&
834
74
         End <= CurLexer->getBuffer().end() &&
835
74
         "module source range not contained within same file buffer");
836
74
  TheModuleLoader.loadModuleFromSource(Loc, ModuleName->getName(),
837
74
                                       StringRef(Start, End - Start));
838
74
}
839
840
7
void Preprocessor::HandlePragmaHdrstop(Token &Tok) {
841
7
  Lex(Tok);
842
7
  if (Tok.is(tok::l_paren)) {
843
2
    Diag(Tok.getLocation(), diag::warn_pp_hdrstop_filename_ignored);
844
2
845
2
    std::string FileName;
846
2
    if (!LexStringLiteral(Tok, FileName, "pragma hdrstop", false))
847
0
      return;
848
2
849
2
    if (Tok.isNot(tok::r_paren)) {
850
0
      Diag(Tok, diag::err_expected) << tok::r_paren;
851
0
      return;
852
0
    }
853
2
    Lex(Tok);
854
2
  }
855
7
  if (Tok.isNot(tok::eod))
856
0
    Diag(Tok.getLocation(), diag::ext_pp_extra_tokens_at_eol)
857
0
        << "pragma hdrstop";
858
7
859
7
  if (creatingPCHWithPragmaHdrStop() &&
860
7
      
SourceMgr.isInMainFile(Tok.getLocation())2
) {
861
2
    assert(CurLexer && "no lexer for #pragma hdrstop processing");
862
2
    Token &Result = Tok;
863
2
    Result.startToken();
864
2
    CurLexer->FormTokenWithChars(Result, CurLexer->BufferEnd, tok::eof);
865
2
    CurLexer->cutOffLexing();
866
2
  }
867
7
  if (usingPCHWithPragmaHdrStop())
868
5
    SkippingUntilPragmaHdrStop = false;
869
7
}
870
871
/// AddPragmaHandler - Add the specified pragma handler to the preprocessor.
872
/// If 'Namespace' is non-null, then it is a token required to exist on the
873
/// pragma line before the pragma string starts, e.g. "STDC" or "GCC".
874
void Preprocessor::AddPragmaHandler(StringRef Namespace,
875
1.89M
                                    PragmaHandler *Handler) {
876
1.89M
  PragmaNamespace *InsertNS = PragmaHandlers.get();
877
1.89M
878
1.89M
  // If this is specified to be in a namespace, step down into it.
879
1.89M
  if (!Namespace.empty()) {
880
1.04M
    // If there is already a pragma handler with the name of this namespace,
881
1.04M
    // we either have an error (directive with the same name as a namespace) or
882
1.04M
    // we already have the namespace to insert into.
883
1.04M
    if (PragmaHandler *Existing = PragmaHandlers->FindHandler(Namespace)) {
884
909k
      InsertNS = Existing->getIfNamespace();
885
909k
      assert(InsertNS != nullptr && "Cannot have a pragma namespace and pragma"
886
909k
             " handler with the same name!");
887
909k
    } else {
888
131k
      // Otherwise, this namespace doesn't exist yet, create and insert the
889
131k
      // handler for it.
890
131k
      InsertNS = new PragmaNamespace(Namespace);
891
131k
      PragmaHandlers->AddPragma(InsertNS);
892
131k
    }
893
1.04M
  }
894
1.89M
895
1.89M
  // Check to make sure we don't already have a pragma for this identifier.
896
1.89M
  assert(!InsertNS->FindHandler(Handler->getName()) &&
897
1.89M
         "Pragma handler already exists for this identifier!");
898
1.89M
  InsertNS->AddPragma(Handler);
899
1.89M
}
900
901
/// RemovePragmaHandler - Remove the specific pragma handler from the
902
/// preprocessor. If \arg Namespace is non-null, then it should be the
903
/// namespace that \arg Handler was added to. It is an error to remove
904
/// a handler that has not been registered.
905
void Preprocessor::RemovePragmaHandler(StringRef Namespace,
906
957k
                                       PragmaHandler *Handler) {
907
957k
  PragmaNamespace *NS = PragmaHandlers.get();
908
957k
909
957k
  // If this is specified to be in a namespace, step down into it.
910
957k
  if (!Namespace.empty()) {
911
419k
    PragmaHandler *Existing = PragmaHandlers->FindHandler(Namespace);
912
419k
    assert(Existing && "Namespace containing handler does not exist!");
913
419k
914
419k
    NS = Existing->getIfNamespace();
915
419k
    assert(NS && "Invalid namespace, registered as a regular pragma handler!");
916
419k
  }
917
957k
918
957k
  NS->RemovePragmaHandler(Handler);
919
957k
920
957k
  // If this is a non-default namespace and it is now empty, remove it.
921
957k
  if (NS != PragmaHandlers.get() && 
NS->IsEmpty()419k
) {
922
43.1k
    PragmaHandlers->RemovePragmaHandler(NS);
923
43.1k
    delete NS;
924
43.1k
  }
925
957k
}
926
927
100
bool Preprocessor::LexOnOffSwitch(tok::OnOffSwitch &Result) {
928
100
  Token Tok;
929
100
  LexUnexpandedToken(Tok);
930
100
931
100
  if (Tok.isNot(tok::identifier)) {
932
4
    Diag(Tok, diag::ext_on_off_switch_syntax);
933
4
    return true;
934
4
  }
935
96
  IdentifierInfo *II = Tok.getIdentifierInfo();
936
96
  if (II->isStr("ON"))
937
15
    Result = tok::OOS_ON;
938
81
  else if (II->isStr("OFF"))
939
73
    Result = tok::OOS_OFF;
940
8
  else if (II->isStr("DEFAULT"))
941
4
    Result = tok::OOS_DEFAULT;
942
4
  else {
943
4
    Diag(Tok, diag::ext_on_off_switch_syntax);
944
4
    return true;
945
4
  }
946
92
947
92
  // Verify that this is followed by EOD.
948
92
  LexUnexpandedToken(Tok);
949
92
  if (Tok.isNot(tok::eod))
950
1
    Diag(Tok, diag::ext_pragma_syntax_eod);
951
92
  return false;
952
92
}
953
954
namespace {
955
956
/// PragmaOnceHandler - "\#pragma once" marks the file as atomically included.
957
struct PragmaOnceHandler : public PragmaHandler {
958
44.2k
  PragmaOnceHandler() : PragmaHandler("once") {}
959
960
  void HandlePragma(Preprocessor &PP, PragmaIntroducer Introducer,
961
116
                    Token &OnceTok) override {
962
116
    PP.CheckEndOfDirective("pragma once");
963
116
    PP.HandlePragmaOnce(OnceTok);
964
116
  }
965
};
966
967
/// PragmaMarkHandler - "\#pragma mark ..." is ignored by the compiler, and the
968
/// rest of the line is not lexed.
969
struct PragmaMarkHandler : public PragmaHandler {
970
44.2k
  PragmaMarkHandler() : PragmaHandler("mark") {}
971
972
  void HandlePragma(Preprocessor &PP, PragmaIntroducer Introducer,
973
1.54k
                    Token &MarkTok) override {
974
1.54k
    PP.HandlePragmaMark();
975
1.54k
  }
976
};
977
978
/// PragmaPoisonHandler - "\#pragma poison x" marks x as not usable.
979
struct PragmaPoisonHandler : public PragmaHandler {
980
88.4k
  PragmaPoisonHandler() : PragmaHandler("poison") {}
981
982
  void HandlePragma(Preprocessor &PP, PragmaIntroducer Introducer,
983
5
                    Token &PoisonTok) override {
984
5
    PP.HandlePragmaPoison();
985
5
  }
986
};
987
988
/// PragmaSystemHeaderHandler - "\#pragma system_header" marks the current file
989
/// as a system header, which silences warnings in it.
990
struct PragmaSystemHeaderHandler : public PragmaHandler {
991
88.4k
  PragmaSystemHeaderHandler() : PragmaHandler("system_header") {}
992
993
  void HandlePragma(Preprocessor &PP, PragmaIntroducer Introducer,
994
28.2k
                    Token &SHToken) override {
995
28.2k
    PP.HandlePragmaSystemHeader(SHToken);
996
28.2k
    PP.CheckEndOfDirective("pragma");
997
28.2k
  }
998
};
999
1000
struct PragmaDependencyHandler : public PragmaHandler {
1001
88.4k
  PragmaDependencyHandler() : PragmaHandler("dependency") {}
1002
1003
  void HandlePragma(Preprocessor &PP, PragmaIntroducer Introducer,
1004
4
                    Token &DepToken) override {
1005
4
    PP.HandlePragmaDependency(DepToken);
1006
4
  }
1007
};
1008
1009
struct PragmaDebugHandler : public PragmaHandler {
1010
44.2k
  PragmaDebugHandler() : PragmaHandler("__debug") {}
1011
1012
  void HandlePragma(Preprocessor &PP, PragmaIntroducer Introducer,
1013
109
                    Token &DebugToken) override {
1014
109
    Token Tok;
1015
109
    PP.LexUnexpandedToken(Tok);
1016
109
    if (Tok.isNot(tok::identifier)) {
1017
0
      PP.Diag(Tok, diag::warn_pragma_diagnostic_invalid);
1018
0
      return;
1019
0
    }
1020
109
    IdentifierInfo *II = Tok.getIdentifierInfo();
1021
109
1022
109
    if (II->isStr("assert")) {
1023
0
      llvm_unreachable("This is an assertion!");
1024
109
    } else if (II->isStr("crash")) {
1025
7
      LLVM_BUILTIN_TRAP;
1026
102
    } else if (II->isStr("parser_crash")) {
1027
14
      Token Crasher;
1028
14
      Crasher.startToken();
1029
14
      Crasher.setKind(tok::annot_pragma_parser_crash);
1030
14
      Crasher.setAnnotationRange(SourceRange(Tok.getLocation()));
1031
14
      PP.EnterToken(Crasher, /*IsReinject*/false);
1032
88
    } else if (II->isStr("dump")) {
1033
19
      Token Identifier;
1034
19
      PP.LexUnexpandedToken(Identifier);
1035
19
      if (auto *DumpII = Identifier.getIdentifierInfo()) {
1036
19
        Token DumpAnnot;
1037
19
        DumpAnnot.startToken();
1038
19
        DumpAnnot.setKind(tok::annot_pragma_dump);
1039
19
        DumpAnnot.setAnnotationRange(
1040
19
            SourceRange(Tok.getLocation(), Identifier.getLocation()));
1041
19
        DumpAnnot.setAnnotationValue(DumpII);
1042
19
        PP.DiscardUntilEndOfDirective();
1043
19
        PP.EnterToken(DumpAnnot, /*IsReinject*/false);
1044
19
      } else {
1045
0
        PP.Diag(Identifier, diag::warn_pragma_debug_missing_argument)
1046
0
            << II->getName();
1047
0
      }
1048
69
    } else if (II->isStr("diag_mapping")) {
1049
0
      Token DiagName;
1050
0
      PP.LexUnexpandedToken(DiagName);
1051
0
      if (DiagName.is(tok::eod))
1052
0
        PP.getDiagnostics().dump();
1053
0
      else if (DiagName.is(tok::string_literal) && !DiagName.hasUDSuffix()) {
1054
0
        StringLiteralParser Literal(DiagName, PP);
1055
0
        if (Literal.hadError)
1056
0
          return;
1057
0
        PP.getDiagnostics().dump(Literal.GetString());
1058
0
      } else {
1059
0
        PP.Diag(DiagName, diag::warn_pragma_debug_missing_argument)
1060
0
            << II->getName();
1061
0
      }
1062
69
    } else if (II->isStr("llvm_fatal_error")) {
1063
0
      llvm::report_fatal_error("#pragma clang __debug llvm_fatal_error");
1064
69
    } else if (II->isStr("llvm_unreachable")) {
1065
0
      llvm_unreachable("#pragma clang __debug llvm_unreachable");
1066
69
    } else if (II->isStr("macro")) {
1067
10
      Token MacroName;
1068
10
      PP.LexUnexpandedToken(MacroName);
1069
10
      auto *MacroII = MacroName.getIdentifierInfo();
1070
10
      if (MacroII)
1071
10
        PP.dumpMacroInfo(MacroII);
1072
0
      else
1073
0
        PP.Diag(MacroName, diag::warn_pragma_debug_missing_argument)
1074
0
            << II->getName();
1075
59
    } else if (II->isStr("module_map")) {
1076
0
      llvm::SmallVector<std::pair<IdentifierInfo *, SourceLocation>, 8>
1077
0
          ModuleName;
1078
0
      if (LexModuleName(PP, Tok, ModuleName))
1079
0
        return;
1080
0
      ModuleMap &MM = PP.getHeaderSearchInfo().getModuleMap();
1081
0
      Module *M = nullptr;
1082
0
      for (auto IIAndLoc : ModuleName) {
1083
0
        M = MM.lookupModuleQualified(IIAndLoc.first->getName(), M);
1084
0
        if (!M) {
1085
0
          PP.Diag(IIAndLoc.second, diag::warn_pragma_debug_unknown_module)
1086
0
              << IIAndLoc.first;
1087
0
          return;
1088
0
        }
1089
0
      }
1090
0
      M->dump();
1091
59
    } else if (II->isStr("overflow_stack")) {
1092
0
      DebugOverflowStack();
1093
59
    } else if (II->isStr("handle_crash")) {
1094
0
      llvm::CrashRecoveryContext *CRC =llvm::CrashRecoveryContext::GetCurrent();
1095
0
      if (CRC)
1096
0
        CRC->HandleCrash();
1097
59
    } else if (II->isStr("captured")) {
1098
59
      HandleCaptured(PP);
1099
59
    } else {
1100
0
      PP.Diag(Tok, diag::warn_pragma_debug_unexpected_command)
1101
0
        << II->getName();
1102
0
    }
1103
109
1104
109
    PPCallbacks *Callbacks = PP.getPPCallbacks();
1105
102
    if (Callbacks)
1106
102
      Callbacks->PragmaDebug(Tok.getLocation(), II->getName());
1107
102
  }
1108
1109
59
  void HandleCaptured(Preprocessor &PP) {
1110
59
    Token Tok;
1111
59
    PP.LexUnexpandedToken(Tok);
1112
59
1113
59
    if (Tok.isNot(tok::eod)) {
1114
1
      PP.Diag(Tok, diag::ext_pp_extra_tokens_at_eol)
1115
1
        << "pragma clang __debug captured";
1116
1
      return;
1117
1
    }
1118
58
1119
58
    SourceLocation NameLoc = Tok.getLocation();
1120
58
    MutableArrayRef<Token> Toks(
1121
58
        PP.getPreprocessorAllocator().Allocate<Token>(1), 1);
1122
58
    Toks[0].startToken();
1123
58
    Toks[0].setKind(tok::annot_pragma_captured);
1124
58
    Toks[0].setLocation(NameLoc);
1125
58
1126
58
    PP.EnterTokenStream(Toks, /*DisableMacroExpansion=*/true,
1127
58
                        /*IsReinject=*/false);
1128
58
  }
1129
1130
// Disable MSVC warning about runtime stack overflow.
1131
#ifdef _MSC_VER
1132
    #pragma warning(disable : 4717)
1133
#endif
1134
0
  static void DebugOverflowStack(void (*P)() = nullptr) {
1135
0
    void (*volatile Self)(void(*P)()) = DebugOverflowStack;
1136
0
    Self(reinterpret_cast<void(*)()>(Self));
1137
0
  }
1138
#ifdef _MSC_VER
1139
    #pragma warning(default : 4717)
1140
#endif
1141
};
1142
1143
/// PragmaDiagnosticHandler - e.g. '\#pragma GCC diagnostic ignored "-Wformat"'
1144
struct PragmaDiagnosticHandler : public PragmaHandler {
1145
private:
1146
  const char *Namespace;
1147
1148
public:
1149
  explicit PragmaDiagnosticHandler(const char *NS)
1150
88.4k
      : PragmaHandler("diagnostic"), Namespace(NS) {}
1151
1152
  void HandlePragma(Preprocessor &PP, PragmaIntroducer Introducer,
1153
852
                    Token &DiagToken) override {
1154
852
    SourceLocation DiagLoc = DiagToken.getLocation();
1155
852
    Token Tok;
1156
852
    PP.LexUnexpandedToken(Tok);
1157
852
    if (Tok.isNot(tok::identifier)) {
1158
0
      PP.Diag(Tok, diag::warn_pragma_diagnostic_invalid);
1159
0
      return;
1160
0
    }
1161
852
    IdentifierInfo *II = Tok.getIdentifierInfo();
1162
852
    PPCallbacks *Callbacks = PP.getPPCallbacks();
1163
852
1164
852
    if (II->isStr("pop")) {
1165
247
      if (!PP.getDiagnostics().popMappings(DiagLoc))
1166
2
        PP.Diag(Tok, diag::warn_pragma_diagnostic_cannot_pop);
1167
245
      else if (Callbacks)
1168
245
        Callbacks->PragmaDiagnosticPop(DiagLoc, Namespace);
1169
247
      return;
1170
605
    } else if (II->isStr("push")) {
1171
247
      PP.getDiagnostics().pushMappings(DiagLoc);
1172
247
      if (Callbacks)
1173
247
        Callbacks->PragmaDiagnosticPush(DiagLoc, Namespace);
1174
247
      return;
1175
247
    }
1176
358
1177
358
    diag::Severity SV = llvm::StringSwitch<diag::Severity>(II->getName())
1178
358
                            .Case("ignored", diag::Severity::Ignored)
1179
358
                            .Case("warning", diag::Severity::Warning)
1180
358
                            .Case("error", diag::Severity::Error)
1181
358
                            .Case("fatal", diag::Severity::Fatal)
1182
358
                            .Default(diag::Severity());
1183
358
1184
358
    if (SV == diag::Severity()) {
1185
3
      PP.Diag(Tok, diag::warn_pragma_diagnostic_invalid);
1186
3
      return;
1187
3
    }
1188
355
1189
355
    PP.LexUnexpandedToken(Tok);
1190
355
    SourceLocation StringLoc = Tok.getLocation();
1191
355
1192
355
    std::string WarningName;
1193
355
    if (!PP.FinishLexStringLiteral(Tok, WarningName, "pragma diagnostic",
1194
355
                                   /*AllowMacroExpansion=*/false))
1195
2
      return;
1196
353
1197
353
    if (Tok.isNot(tok::eod)) {
1198
2
      PP.Diag(Tok.getLocation(), diag::warn_pragma_diagnostic_invalid_token);
1199
2
      return;
1200
2
    }
1201
351
1202
351
    if (WarningName.size() < 3 || WarningName[0] != '-' ||
1203
351
        
(349
WarningName[1] != 'W'349
&&
WarningName[1] != 'R'0
)) {
1204
2
      PP.Diag(StringLoc, diag::warn_pragma_diagnostic_invalid_option);
1205
2
      return;
1206
2
    }
1207
349
1208
349
    diag::Flavor Flavor = WarningName[1] == 'W' ? diag::Flavor::WarningOrError
1209
349
                                                : 
diag::Flavor::Remark0
;
1210
349
    StringRef Group = StringRef(WarningName).substr(2);
1211
349
    bool unknownDiag = false;
1212
349
    if (Group == "everything") {
1213
19
      // Special handling for pragma clang diagnostic ... "-Weverything".
1214
19
      // There is no formal group named "everything", so there has to be a
1215
19
      // special case for it.
1216
19
      PP.getDiagnostics().setSeverityForAll(Flavor, SV, DiagLoc);
1217
19
    } else
1218
330
      unknownDiag = PP.getDiagnostics().setSeverityForGroup(Flavor, Group, SV,
1219
330
                                                            DiagLoc);
1220
349
    if (unknownDiag)
1221
4
      PP.Diag(StringLoc, diag::warn_pragma_diagnostic_unknown_warning)
1222
4
        << WarningName;
1223
345
    else if (Callbacks)
1224
345
      Callbacks->PragmaDiagnostic(DiagLoc, Namespace, SV, WarningName);
1225
349
  }
1226
};
1227
1228
/// "\#pragma hdrstop [<header-name-string>]"
1229
struct PragmaHdrstopHandler : public PragmaHandler {
1230
2.31k
  PragmaHdrstopHandler() : PragmaHandler("hdrstop") {}
1231
  void HandlePragma(Preprocessor &PP, PragmaIntroducer Introducer,
1232
2
                    Token &DepToken) override {
1233
2
    PP.HandlePragmaHdrstop(DepToken);
1234
2
  }
1235
};
1236
1237
/// "\#pragma warning(...)".  MSVC's diagnostics do not map cleanly to clang's
1238
/// diagnostics, so we don't really implement this pragma.  We parse it and
1239
/// ignore it to avoid -Wunknown-pragma warnings.
1240
struct PragmaWarningHandler : public PragmaHandler {
1241
2.31k
  PragmaWarningHandler() : PragmaHandler("warning") {}
1242
1243
  void HandlePragma(Preprocessor &PP, PragmaIntroducer Introducer,
1244
73
                    Token &Tok) override {
1245
73
    // Parse things like:
1246
73
    // warning(push, 1)
1247
73
    // warning(pop)
1248
73
    // warning(disable : 1 2 3 ; error : 4 5 6 ; suppress : 7 8 9)
1249
73
    SourceLocation DiagLoc = Tok.getLocation();
1250
73
    PPCallbacks *Callbacks = PP.getPPCallbacks();
1251
73
1252
73
    PP.Lex(Tok);
1253
73
    if (Tok.isNot(tok::l_paren)) {
1254
2
      PP.Diag(Tok, diag::warn_pragma_warning_expected) << "(";
1255
2
      return;
1256
2
    }
1257
71
1258
71
    PP.Lex(Tok);
1259
71
    IdentifierInfo *II = Tok.getIdentifierInfo();
1260
71
1261
71
    if (II && 
II->isStr("push")59
) {
1262
28
      // #pragma warning( push[ ,n ] )
1263
28
      int Level = -1;
1264
28
      PP.Lex(Tok);
1265
28
      if (Tok.is(tok::comma)) {
1266
17
        PP.Lex(Tok);
1267
17
        uint64_t Value;
1268
17
        if (Tok.is(tok::numeric_constant) &&
1269
17
            
PP.parseSimpleIntegerLiteral(Tok, Value)15
)
1270
14
          Level = int(Value);
1271
17
        if (Level < 0 || 
Level > 414
) {
1272
5
          PP.Diag(Tok, diag::warn_pragma_warning_push_level);
1273
5
          return;
1274
5
        }
1275
23
      }
1276
23
      if (Callbacks)
1277
23
        Callbacks->PragmaWarningPush(DiagLoc, Level);
1278
43
    } else if (II && 
II->isStr("pop")31
) {
1279
9
      // #pragma warning( pop )
1280
9
      PP.Lex(Tok);
1281
9
      if (Callbacks)
1282
9
        Callbacks->PragmaWarningPop(DiagLoc);
1283
34
    } else {
1284
34
      // #pragma warning( warning-specifier : warning-number-list
1285
34
      //                  [; warning-specifier : warning-number-list...] )
1286
40
      while (true) {
1287
40
        II = Tok.getIdentifierInfo();
1288
40
        if (!II && 
!Tok.is(tok::numeric_constant)14
) {
1289
4
          PP.Diag(Tok, diag::warn_pragma_warning_spec_invalid);
1290
4
          return;
1291
4
        }
1292
36
1293
36
        // Figure out which warning specifier this is.
1294
36
        bool SpecifierValid;
1295
36
        StringRef Specifier;
1296
36
        llvm::SmallString<1> SpecifierBuf;
1297
36
        if (II) {
1298
26
          Specifier = II->getName();
1299
26
          SpecifierValid = llvm::StringSwitch<bool>(Specifier)
1300
26
                               .Cases("default", "disable", "error", "once",
1301
26
                                      "suppress", true)
1302
26
                               .Default(false);
1303
26
          // If we read a correct specifier, snatch next token (that should be
1304
26
          // ":", checked later).
1305
26
          if (SpecifierValid)
1306
24
            PP.Lex(Tok);
1307
26
        } else {
1308
10
          // Token is a numeric constant. It should be either 1, 2, 3 or 4.
1309
10
          uint64_t Value;
1310
10
          Specifier = PP.getSpelling(Tok, SpecifierBuf);
1311
10
          if (PP.parseSimpleIntegerLiteral(Tok, Value)) {
1312
10
            SpecifierValid = (Value >= 1) && (Value <= 4);
1313
10
          } else
1314
0
            SpecifierValid = false;
1315
10
          // Next token already snatched by parseSimpleIntegerLiteral.
1316
10
        }
1317
36
1318
36
        if (!SpecifierValid) {
1319
4
          PP.Diag(Tok, diag::warn_pragma_warning_spec_invalid);
1320
4
          return;
1321
4
        }
1322
32
        if (Tok.isNot(tok::colon)) {
1323
4
          PP.Diag(Tok, diag::warn_pragma_warning_expected) << ":";
1324
4
          return;
1325
4
        }
1326
28
1327
28
        // Collect the warning ids.
1328
28
        SmallVector<int, 4> Ids;
1329
28
        PP.Lex(Tok);
1330
66
        while (Tok.is(tok::numeric_constant)) {
1331
40
          uint64_t Value;
1332
40
          if (!PP.parseSimpleIntegerLiteral(Tok, Value) || Value == 0 ||
1333
40
              
Value > std::numeric_limits<int>::max()38
) {
1334
2
            PP.Diag(Tok, diag::warn_pragma_warning_expected_number);
1335
2
            return;
1336
2
          }
1337
38
          Ids.push_back(int(Value));
1338
38
        }
1339
28
        
if (26
Callbacks26
)
1340
26
          Callbacks->PragmaWarning(DiagLoc, Specifier, Ids);
1341
26
1342
26
        // Parse the next specifier if there is a semicolon.
1343
26
        if (Tok.isNot(tok::semi))
1344
20
          break;
1345
6
        PP.Lex(Tok);
1346
6
      }
1347
34
    }
1348
71
1349
71
    
if (52
Tok.isNot(tok::r_paren)52
) {
1350
6
      PP.Diag(Tok, diag::warn_pragma_warning_expected) << ")";
1351
6
      return;
1352
6
    }
1353
46
1354
46
    PP.Lex(Tok);
1355
46
    if (Tok.isNot(tok::eod))
1356
2
      PP.Diag(Tok, diag::ext_pp_extra_tokens_at_eol) << "pragma warning";
1357
46
  }
1358
};
1359
1360
/// "\#pragma execution_character_set(...)". MSVC supports this pragma only
1361
/// for "UTF-8". We parse it and ignore it if UTF-8 is provided and warn
1362
/// otherwise to avoid -Wunknown-pragma warnings.
1363
struct PragmaExecCharsetHandler : public PragmaHandler {
1364
2.31k
  PragmaExecCharsetHandler() : PragmaHandler("execution_character_set") {}
1365
1366
  void HandlePragma(Preprocessor &PP, PragmaIntroducer Introducer,
1367
32
                    Token &Tok) override {
1368
32
    // Parse things like:
1369
32
    // execution_character_set(push, "UTF-8")
1370
32
    // execution_character_set(pop)
1371
32
    SourceLocation DiagLoc = Tok.getLocation();
1372
32
    PPCallbacks *Callbacks = PP.getPPCallbacks();
1373
32
1374
32
    PP.Lex(Tok);
1375
32
    if (Tok.isNot(tok::l_paren)) {
1376
2
      PP.Diag(Tok, diag::warn_pragma_exec_charset_expected) << "(";
1377
2
      return;
1378
2
    }
1379
30
1380
30
    PP.Lex(Tok);
1381
30
    IdentifierInfo *II = Tok.getIdentifierInfo();
1382
30
1383
30
    if (II && 
II->isStr("push")26
) {
1384
16
      // #pragma execution_character_set( push[ , string ] )
1385
16
      PP.Lex(Tok);
1386
16
      if (Tok.is(tok::comma)) {
1387
12
        PP.Lex(Tok);
1388
12
1389
12
        std::string ExecCharset;
1390
12
        if (!PP.FinishLexStringLiteral(Tok, ExecCharset,
1391
12
                                       "pragma execution_character_set",
1392
12
                                       /*AllowMacroExpansion=*/false))
1393
6
          return;
1394
6
1395
6
        // MSVC supports either of these, but nothing else.
1396
6
        if (ExecCharset != "UTF-8" && 
ExecCharset != "utf-8"4
) {
1397
2
          PP.Diag(Tok, diag::warn_pragma_exec_charset_push_invalid) << ExecCharset;
1398
2
          return;
1399
2
        }
1400
8
      }
1401
8
      if (Callbacks)
1402
8
        Callbacks->PragmaExecCharsetPush(DiagLoc, "UTF-8");
1403
14
    } else if (II && 
II->isStr("pop")10
) {
1404
6
      // #pragma execution_character_set( pop )
1405
6
      PP.Lex(Tok);
1406
6
      if (Callbacks)
1407
6
        Callbacks->PragmaExecCharsetPop(DiagLoc);
1408
8
    } else {
1409
8
      PP.Diag(Tok, diag::warn_pragma_exec_charset_spec_invalid);
1410
8
      return;
1411
8
    }
1412
14
1413
14
    if (Tok.isNot(tok::r_paren)) {
1414
6
      PP.Diag(Tok, diag::warn_pragma_exec_charset_expected) << ")";
1415
6
      return;
1416
6
    }
1417
8
1418
8
    PP.Lex(Tok);
1419
8
    if (Tok.isNot(tok::eod))
1420
0
      PP.Diag(Tok, diag::ext_pp_extra_tokens_at_eol) << "pragma execution_character_set";
1421
8
  }
1422
};
1423
1424
/// PragmaIncludeAliasHandler - "\#pragma include_alias("...")".
1425
struct PragmaIncludeAliasHandler : public PragmaHandler {
1426
2.31k
  PragmaIncludeAliasHandler() : PragmaHandler("include_alias") {}
1427
1428
  void HandlePragma(Preprocessor &PP, PragmaIntroducer Introducer,
1429
22
                    Token &IncludeAliasTok) override {
1430
22
    PP.HandlePragmaIncludeAlias(IncludeAliasTok);
1431
22
  }
1432
};
1433
1434
/// PragmaMessageHandler - Handle the microsoft and gcc \#pragma message
1435
/// extension.  The syntax is:
1436
/// \code
1437
///   #pragma message(string)
1438
/// \endcode
1439
/// OR, in GCC mode:
1440
/// \code
1441
///   #pragma message string
1442
/// \endcode
1443
/// string is a string, which is fully macro expanded, and permits string
1444
/// concatenation, embedded escape characters, etc... See MSDN for more details.
1445
/// Also handles \#pragma GCC warning and \#pragma GCC error which take the same
1446
/// form as \#pragma message.
1447
struct PragmaMessageHandler : public PragmaHandler {
1448
private:
1449
  const PPCallbacks::PragmaMessageKind Kind;
1450
  const StringRef Namespace;
1451
1452
  static const char* PragmaKind(PPCallbacks::PragmaMessageKind Kind,
1453
132k
                                bool PragmaNameOnly = false) {
1454
132k
    switch (Kind) {
1455
132k
      case PPCallbacks::PMK_Message:
1456
44.2k
        return PragmaNameOnly ? 
"message"44.2k
:
"pragma message"12
;
1457
132k
      case PPCallbacks::PMK_Warning:
1458
44.2k
        return PragmaNameOnly ? 
"warning"44.2k
:
"pragma warning"7
;
1459
132k
      case PPCallbacks::PMK_Error:
1460
44.2k
        return PragmaNameOnly ? 
"error"44.2k
:
"pragma error"6
;
1461
0
    }
1462
0
    llvm_unreachable("Unknown PragmaMessageKind!");
1463
0
  }
1464
1465
public:
1466
  PragmaMessageHandler(PPCallbacks::PragmaMessageKind Kind,
1467
                       StringRef Namespace = StringRef())
1468
      : PragmaHandler(PragmaKind(Kind, true)), Kind(Kind),
1469
132k
        Namespace(Namespace) {}
1470
1471
  void HandlePragma(Preprocessor &PP, PragmaIntroducer Introducer,
1472
26
                    Token &Tok) override {
1473
26
    SourceLocation MessageLoc = Tok.getLocation();
1474
26
    PP.Lex(Tok);
1475
26
    bool ExpectClosingParen = false;
1476
26
    switch (Tok.getKind()) {
1477
26
    case tok::l_paren:
1478
11
      // We have a MSVC style pragma message.
1479
11
      ExpectClosingParen = true;
1480
11
      // Read the string.
1481
11
      PP.Lex(Tok);
1482
11
      break;
1483
26
    case tok::string_literal:
1484
14
      // We have a GCC style pragma message, and we just read the string.
1485
14
      break;
1486
26
    default:
1487
1
      PP.Diag(MessageLoc, diag::err_pragma_message_malformed) << Kind;
1488
1
      return;
1489
25
    }
1490
25
1491
25
    std::string MessageString;
1492
25
    if (!PP.FinishLexStringLiteral(Tok, MessageString, PragmaKind(Kind),
1493
25
                                   /*AllowMacroExpansion=*/true))
1494
2
      return;
1495
23
1496
23
    if (ExpectClosingParen) {
1497
9
      if (Tok.isNot(tok::r_paren)) {
1498
1
        PP.Diag(Tok.getLocation(), diag::err_pragma_message_malformed) << Kind;
1499
1
        return;
1500
1
      }
1501
8
      PP.Lex(Tok);  // eat the r_paren.
1502
8
    }
1503
23
1504
23
    
if (22
Tok.isNot(tok::eod)22
) {
1505
0
      PP.Diag(Tok.getLocation(), diag::err_pragma_message_malformed) << Kind;
1506
0
      return;
1507
0
    }
1508
22
1509
22
    // Output the message.
1510
22
    PP.Diag(MessageLoc, (Kind == PPCallbacks::PMK_Error)
1511
22
                          ? 
diag::err_pragma_message5
1512
22
                          : 
diag::warn_pragma_message17
) << MessageString;
1513
22
1514
22
    // If the pragma is lexically sound, notify any interested PPCallbacks.
1515
22
    if (PPCallbacks *Callbacks = PP.getPPCallbacks())
1516
22
      Callbacks->PragmaMessage(MessageLoc, Namespace, Kind, MessageString);
1517
22
  }
1518
};
1519
1520
/// Handle the clang \#pragma module import extension. The syntax is:
1521
/// \code
1522
///   #pragma clang module import some.module.name
1523
/// \endcode
1524
struct PragmaModuleImportHandler : public PragmaHandler {
1525
44.2k
  PragmaModuleImportHandler() : PragmaHandler("import") {}
1526
1527
  void HandlePragma(Preprocessor &PP, PragmaIntroducer Introducer,
1528
118
                    Token &Tok) override {
1529
118
    SourceLocation ImportLoc = Tok.getLocation();
1530
118
1531
118
    // Read the module name.
1532
118
    llvm::SmallVector<std::pair<IdentifierInfo *, SourceLocation>, 8>
1533
118
        ModuleName;
1534
118
    if (LexModuleName(PP, Tok, ModuleName))
1535
6
      return;
1536
112
1537
112
    if (Tok.isNot(tok::eod))
1538
2
      PP.Diag(Tok, diag::ext_pp_extra_tokens_at_eol) << "pragma";
1539
112
1540
112
    // If we have a non-empty module path, load the named module.
1541
112
    Module *Imported =
1542
112
        PP.getModuleLoader().loadModule(ImportLoc, ModuleName, Module::Hidden,
1543
112
                                      /*IsInclusionDirective=*/false);
1544
112
    if (!Imported)
1545
0
      return;
1546
112
1547
112
    PP.makeModuleVisible(Imported, ImportLoc);
1548
112
    PP.EnterAnnotationToken(SourceRange(ImportLoc, ModuleName.back().second),
1549
112
                            tok::annot_module_include, Imported);
1550
112
    if (auto *CB = PP.getPPCallbacks())
1551
112
      CB->moduleImport(ImportLoc, ModuleName, Imported);
1552
112
  }
1553
};
1554
1555
/// Handle the clang \#pragma module begin extension. The syntax is:
1556
/// \code
1557
///   #pragma clang module begin some.module.name
1558
///   ...
1559
///   #pragma clang module end
1560
/// \endcode
1561
struct PragmaModuleBeginHandler : public PragmaHandler {
1562
44.2k
  PragmaModuleBeginHandler() : PragmaHandler("begin") {}
1563
1564
  void HandlePragma(Preprocessor &PP, PragmaIntroducer Introducer,
1565
144
                    Token &Tok) override {
1566
144
    SourceLocation BeginLoc = Tok.getLocation();
1567
144
1568
144
    // Read the module name.
1569
144
    llvm::SmallVector<std::pair<IdentifierInfo *, SourceLocation>, 8>
1570
144
        ModuleName;
1571
144
    if (LexModuleName(PP, Tok, ModuleName))
1572
2
      return;
1573
142
1574
142
    if (Tok.isNot(tok::eod))
1575
2
      PP.Diag(Tok, diag::ext_pp_extra_tokens_at_eol) << "pragma";
1576
142
1577
142
    // We can only enter submodules of the current module.
1578
142
    StringRef Current = PP.getLangOpts().CurrentModule;
1579
142
    if (ModuleName.front().first->getName() != Current) {
1580
0
      PP.Diag(ModuleName.front().second, diag::err_pp_module_begin_wrong_module)
1581
0
        << ModuleName.front().first << (ModuleName.size() > 1)
1582
0
        << Current.empty() << Current;
1583
0
      return;
1584
0
    }
1585
142
1586
142
    // Find the module we're entering. We require that a module map for it
1587
142
    // be loaded or implicitly loadable.
1588
142
    auto &HSI = PP.getHeaderSearchInfo();
1589
142
    Module *M = HSI.lookupModule(Current);
1590
142
    if (!M) {
1591
0
      PP.Diag(ModuleName.front().second,
1592
0
              diag::err_pp_module_begin_no_module_map) << Current;
1593
0
      return;
1594
0
    }
1595
189
    
for (unsigned I = 1; 142
I != ModuleName.size();
++I47
) {
1596
47
      auto *NewM = M->findOrInferSubmodule(ModuleName[I].first->getName());
1597
47
      if (!NewM) {
1598
0
        PP.Diag(ModuleName[I].second, diag::err_pp_module_begin_no_submodule)
1599
0
          << M->getFullModuleName() << ModuleName[I].first;
1600
0
        return;
1601
0
      }
1602
47
      M = NewM;
1603
47
    }
1604
142
1605
142
    // If the module isn't available, it doesn't make sense to enter it.
1606
142
    if (Preprocessor::checkModuleIsAvailable(
1607
142
            PP.getLangOpts(), PP.getTargetInfo(), PP.getDiagnostics(), M)) {
1608
1
      PP.Diag(BeginLoc, diag::note_pp_module_begin_here)
1609
1
        << M->getTopLevelModuleName();
1610
1
      return;
1611
1
    }
1612
141
1613
141
    // Enter the scope of the submodule.
1614
141
    PP.EnterSubmodule(M, BeginLoc, /*ForPragma*/true);
1615
141
    PP.EnterAnnotationToken(SourceRange(BeginLoc, ModuleName.back().second),
1616
141
                            tok::annot_module_begin, M);
1617
141
  }
1618
};
1619
1620
/// Handle the clang \#pragma module end extension.
1621
struct PragmaModuleEndHandler : public PragmaHandler {
1622
44.2k
  PragmaModuleEndHandler() : PragmaHandler("end") {}
1623
1624
  void HandlePragma(Preprocessor &PP, PragmaIntroducer Introducer,
1625
142
                    Token &Tok) override {
1626
142
    SourceLocation Loc = Tok.getLocation();
1627
142
1628
142
    PP.LexUnexpandedToken(Tok);
1629
142
    if (Tok.isNot(tok::eod))
1630
2
      PP.Diag(Tok, diag::ext_pp_extra_tokens_at_eol) << "pragma";
1631
142
1632
142
    Module *M = PP.LeaveSubmodule(/*ForPragma*/true);
1633
142
    if (M)
1634
139
      PP.EnterAnnotationToken(SourceRange(Loc), tok::annot_module_end, M);
1635
3
    else
1636
3
      PP.Diag(Loc, diag::err_pp_module_end_without_module_begin);
1637
142
  }
1638
};
1639
1640
/// Handle the clang \#pragma module build extension.
1641
struct PragmaModuleBuildHandler : public PragmaHandler {
1642
44.2k
  PragmaModuleBuildHandler() : PragmaHandler("build") {}
1643
1644
  void HandlePragma(Preprocessor &PP, PragmaIntroducer Introducer,
1645
75
                    Token &Tok) override {
1646
75
    PP.HandlePragmaModuleBuild(Tok);
1647
75
  }
1648
};
1649
1650
/// Handle the clang \#pragma module load extension.
1651
struct PragmaModuleLoadHandler : public PragmaHandler {
1652
44.2k
  PragmaModuleLoadHandler() : PragmaHandler("load") {}
1653
1654
  void HandlePragma(Preprocessor &PP, PragmaIntroducer Introducer,
1655
6
                    Token &Tok) override {
1656
6
    SourceLocation Loc = Tok.getLocation();
1657
6
1658
6
    // Read the module name.
1659
6
    llvm::SmallVector<std::pair<IdentifierInfo *, SourceLocation>, 8>
1660
6
        ModuleName;
1661
6
    if (LexModuleName(PP, Tok, ModuleName))
1662
0
      return;
1663
6
1664
6
    if (Tok.isNot(tok::eod))
1665
0
      PP.Diag(Tok, diag::ext_pp_extra_tokens_at_eol) << "pragma";
1666
6
1667
6
    // Load the module, don't make it visible.
1668
6
    PP.getModuleLoader().loadModule(Loc, ModuleName, Module::Hidden,
1669
6
                                    /*IsInclusionDirective=*/false);
1670
6
  }
1671
};
1672
1673
/// PragmaPushMacroHandler - "\#pragma push_macro" saves the value of the
1674
/// macro on the top of the stack.
1675
struct PragmaPushMacroHandler : public PragmaHandler {
1676
44.2k
  PragmaPushMacroHandler() : PragmaHandler("push_macro") {}
1677
1678
  void HandlePragma(Preprocessor &PP, PragmaIntroducer Introducer,
1679
12.3k
                    Token &PushMacroTok) override {
1680
12.3k
    PP.HandlePragmaPushMacro(PushMacroTok);
1681
12.3k
  }
1682
};
1683
1684
/// PragmaPopMacroHandler - "\#pragma pop_macro" sets the value of the
1685
/// macro to the value on the top of the stack.
1686
struct PragmaPopMacroHandler : public PragmaHandler {
1687
44.2k
  PragmaPopMacroHandler() : PragmaHandler("pop_macro") {}
1688
1689
  void HandlePragma(Preprocessor &PP, PragmaIntroducer Introducer,
1690
12.3k
                    Token &PopMacroTok) override {
1691
12.3k
    PP.HandlePragmaPopMacro(PopMacroTok);
1692
12.3k
  }
1693
};
1694
1695
/// PragmaARCCFCodeAuditedHandler -
1696
///   \#pragma clang arc_cf_code_audited begin/end
1697
struct PragmaARCCFCodeAuditedHandler : public PragmaHandler {
1698
44.2k
  PragmaARCCFCodeAuditedHandler() : PragmaHandler("arc_cf_code_audited") {}
1699
1700
  void HandlePragma(Preprocessor &PP, PragmaIntroducer Introducer,
1701
1.87k
                    Token &NameTok) override {
1702
1.87k
    SourceLocation Loc = NameTok.getLocation();
1703
1.87k
    bool IsBegin;
1704
1.87k
1705
1.87k
    Token Tok;
1706
1.87k
1707
1.87k
    // Lex the 'begin' or 'end'.
1708
1.87k
    PP.LexUnexpandedToken(Tok);
1709
1.87k
    const IdentifierInfo *BeginEnd = Tok.getIdentifierInfo();
1710
1.87k
    if (BeginEnd && BeginEnd->isStr("begin")) {
1711
938
      IsBegin = true;
1712
938
    } else 
if (936
BeginEnd936
&&
BeginEnd->isStr("end")936
) {
1713
935
      IsBegin = false;
1714
935
    } else {
1715
1
      PP.Diag(Tok.getLocation(), diag::err_pp_arc_cf_code_audited_syntax);
1716
1
      return;
1717
1
    }
1718
1.87k
1719
1.87k
    // Verify that this is followed by EOD.
1720
1.87k
    PP.LexUnexpandedToken(Tok);
1721
1.87k
    if (Tok.isNot(tok::eod))
1722
1
      PP.Diag(Tok, diag::ext_pp_extra_tokens_at_eol) << "pragma";
1723
1.87k
1724
1.87k
    // The start location of the active audit.
1725
1.87k
    SourceLocation BeginLoc = PP.getPragmaARCCFCodeAuditedLoc();
1726
1.87k
1727
1.87k
    // The start location we want after processing this.
1728
1.87k
    SourceLocation NewLoc;
1729
1.87k
1730
1.87k
    if (IsBegin) {
1731
938
      // Complain about attempts to re-enter an audit.
1732
938
      if (BeginLoc.isValid()) {
1733
1
        PP.Diag(Loc, diag::err_pp_double_begin_of_arc_cf_code_audited);
1734
1
        PP.Diag(BeginLoc, diag::note_pragma_entered_here);
1735
1
      }
1736
938
      NewLoc = Loc;
1737
938
    } else {
1738
935
      // Complain about attempts to leave an audit that doesn't exist.
1739
935
      if (!BeginLoc.isValid()) {
1740
1
        PP.Diag(Loc, diag::err_pp_unmatched_end_of_arc_cf_code_audited);
1741
1
        return;
1742
1
      }
1743
934
      NewLoc = SourceLocation();
1744
934
    }
1745
1.87k
1746
1.87k
    PP.setPragmaARCCFCodeAuditedLoc(NewLoc);
1747
1.87k
  }
1748
};
1749
1750
/// PragmaAssumeNonNullHandler -
1751
///   \#pragma clang assume_nonnull begin/end
1752
struct PragmaAssumeNonNullHandler : public PragmaHandler {
1753
44.2k
  PragmaAssumeNonNullHandler() : PragmaHandler("assume_nonnull") {}
1754
1755
  void HandlePragma(Preprocessor &PP, PragmaIntroducer Introducer,
1756
8.63k
                    Token &NameTok) override {
1757
8.63k
    SourceLocation Loc = NameTok.getLocation();
1758
8.63k
    bool IsBegin;
1759
8.63k
1760
8.63k
    Token Tok;
1761
8.63k
1762
8.63k
    // Lex the 'begin' or 'end'.
1763
8.63k
    PP.LexUnexpandedToken(Tok);
1764
8.63k
    const IdentifierInfo *BeginEnd = Tok.getIdentifierInfo();
1765
8.63k
    if (BeginEnd && BeginEnd->isStr("begin")) {
1766
4.31k
      IsBegin = true;
1767
4.31k
    } else 
if (4.31k
BeginEnd4.31k
&&
BeginEnd->isStr("end")4.31k
) {
1768
4.31k
      IsBegin = false;
1769
4.31k
    } else {
1770
1
      PP.Diag(Tok.getLocation(), diag::err_pp_assume_nonnull_syntax);
1771
1
      return;
1772
1
    }
1773
8.62k
1774
8.62k
    // Verify that this is followed by EOD.
1775
8.62k
    PP.LexUnexpandedToken(Tok);
1776
8.62k
    if (Tok.isNot(tok::eod))
1777
0
      PP.Diag(Tok, diag::ext_pp_extra_tokens_at_eol) << "pragma";
1778
8.62k
1779
8.62k
    // The start location of the active audit.
1780
8.62k
    SourceLocation BeginLoc = PP.getPragmaAssumeNonNullLoc();
1781
8.62k
1782
8.62k
    // The start location we want after processing this.
1783
8.62k
    SourceLocation NewLoc;
1784
8.62k
    PPCallbacks *Callbacks = PP.getPPCallbacks();
1785
8.62k
1786
8.62k
    if (IsBegin) {
1787
4.31k
      // Complain about attempts to re-enter an audit.
1788
4.31k
      if (BeginLoc.isValid()) {
1789
1
        PP.Diag(Loc, diag::err_pp_double_begin_of_assume_nonnull);
1790
1
        PP.Diag(BeginLoc, diag::note_pragma_entered_here);
1791
1
      }
1792
4.31k
      NewLoc = Loc;
1793
4.31k
      if (Callbacks)
1794
4.31k
        Callbacks->PragmaAssumeNonNullBegin(NewLoc);
1795
4.31k
    } else {
1796
4.31k
      // Complain about attempts to leave an audit that doesn't exist.
1797
4.31k
      if (!BeginLoc.isValid()) {
1798
0
        PP.Diag(Loc, diag::err_pp_unmatched_end_of_assume_nonnull);
1799
0
        return;
1800
0
      }
1801
4.31k
      NewLoc = SourceLocation();
1802
4.31k
      if (Callbacks)
1803
4.31k
        Callbacks->PragmaAssumeNonNullEnd(NewLoc);
1804
4.31k
    }
1805
8.62k
1806
8.62k
    PP.setPragmaAssumeNonNullLoc(NewLoc);
1807
8.62k
  }
1808
};
1809
1810
/// Handle "\#pragma region [...]"
1811
///
1812
/// The syntax is
1813
/// \code
1814
///   #pragma region [optional name]
1815
///   #pragma endregion [optional comment]
1816
/// \endcode
1817
///
1818
/// \note This is
1819
/// <a href="http://msdn.microsoft.com/en-us/library/b6xkz944(v=vs.80).aspx">editor-only</a>
1820
/// pragma, just skipped by compiler.
1821
struct PragmaRegionHandler : public PragmaHandler {
1822
88.4k
  PragmaRegionHandler(const char *pragma) : PragmaHandler(pragma) {}
1823
1824
  void HandlePragma(Preprocessor &PP, PragmaIntroducer Introducer,
1825
11
                    Token &NameTok) override {
1826
11
    // #pragma region: endregion matches can be verified
1827
11
    // __pragma(region): no sense, but ignored by msvc
1828
11
    // _Pragma is not valid for MSVC, but there isn't any point
1829
11
    // to handle a _Pragma differently.
1830
11
  }
1831
};
1832
1833
} // namespace
1834
1835
/// RegisterBuiltinPragmas - Install the standard preprocessor pragmas:
1836
/// \#pragma GCC poison/system_header/dependency and \#pragma once.
1837
44.2k
void Preprocessor::RegisterBuiltinPragmas() {
1838
44.2k
  AddPragmaHandler(new PragmaOnceHandler());
1839
44.2k
  AddPragmaHandler(new PragmaMarkHandler());
1840
44.2k
  AddPragmaHandler(new PragmaPushMacroHandler());
1841
44.2k
  AddPragmaHandler(new PragmaPopMacroHandler());
1842
44.2k
  AddPragmaHandler(new PragmaMessageHandler(PPCallbacks::PMK_Message));
1843
44.2k
1844
44.2k
  // #pragma GCC ...
1845
44.2k
  AddPragmaHandler("GCC", new PragmaPoisonHandler());
1846
44.2k
  AddPragmaHandler("GCC", new PragmaSystemHeaderHandler());
1847
44.2k
  AddPragmaHandler("GCC", new PragmaDependencyHandler());
1848
44.2k
  AddPragmaHandler("GCC", new PragmaDiagnosticHandler("GCC"));
1849
44.2k
  AddPragmaHandler("GCC", new PragmaMessageHandler(PPCallbacks::PMK_Warning,
1850
44.2k
                                                   "GCC"));
1851
44.2k
  AddPragmaHandler("GCC", new PragmaMessageHandler(PPCallbacks::PMK_Error,
1852
44.2k
                                                   "GCC"));
1853
44.2k
  // #pragma clang ...
1854
44.2k
  AddPragmaHandler("clang", new PragmaPoisonHandler());
1855
44.2k
  AddPragmaHandler("clang", new PragmaSystemHeaderHandler());
1856
44.2k
  AddPragmaHandler("clang", new PragmaDebugHandler());
1857
44.2k
  AddPragmaHandler("clang", new PragmaDependencyHandler());
1858
44.2k
  AddPragmaHandler("clang", new PragmaDiagnosticHandler("clang"));
1859
44.2k
  AddPragmaHandler("clang", new PragmaARCCFCodeAuditedHandler());
1860
44.2k
  AddPragmaHandler("clang", new PragmaAssumeNonNullHandler());
1861
44.2k
1862
44.2k
  // #pragma clang module ...
1863
44.2k
  auto *ModuleHandler = new PragmaNamespace("module");
1864
44.2k
  AddPragmaHandler("clang", ModuleHandler);
1865
44.2k
  ModuleHandler->AddPragma(new PragmaModuleImportHandler());
1866
44.2k
  ModuleHandler->AddPragma(new PragmaModuleBeginHandler());
1867
44.2k
  ModuleHandler->AddPragma(new PragmaModuleEndHandler());
1868
44.2k
  ModuleHandler->AddPragma(new PragmaModuleBuildHandler());
1869
44.2k
  ModuleHandler->AddPragma(new PragmaModuleLoadHandler());
1870
44.2k
1871
44.2k
  // Add region pragmas.
1872
44.2k
  AddPragmaHandler(new PragmaRegionHandler("region"));
1873
44.2k
  AddPragmaHandler(new PragmaRegionHandler("endregion"));
1874
44.2k
1875
44.2k
  // MS extensions.
1876
44.2k
  if (LangOpts.MicrosoftExt) {
1877
2.31k
    AddPragmaHandler(new PragmaWarningHandler());
1878
2.31k
    AddPragmaHandler(new PragmaExecCharsetHandler());
1879
2.31k
    AddPragmaHandler(new PragmaIncludeAliasHandler());
1880
2.31k
    AddPragmaHandler(new PragmaHdrstopHandler());
1881
2.31k
  }
1882
44.2k
1883
44.2k
  // Pragmas added by plugins
1884
44.2k
  for (PragmaHandlerRegistry::iterator it = PragmaHandlerRegistry::begin(),
1885
44.2k
                                       ie = PragmaHandlerRegistry::end();
1886
44.2k
       it != ie; 
++it0
) {
1887
0
    AddPragmaHandler(it->instantiate().release());
1888
0
  }
1889
44.2k
}
1890
1891
/// Ignore all pragmas, useful for modes such as -Eonly which would otherwise
1892
/// warn about those pragmas being unknown.
1893
1.32k
void Preprocessor::IgnorePragmas() {
1894
1.32k
  AddPragmaHandler(new EmptyPragmaHandler());
1895
1.32k
  // Also ignore all pragmas in all namespaces created
1896
1.32k
  // in Preprocessor::RegisterBuiltinPragmas().
1897
1.32k
  AddPragmaHandler("GCC", new EmptyPragmaHandler());
1898
1.32k
  AddPragmaHandler("clang", new EmptyPragmaHandler());
1899
1.32k
}