Coverage Report

Created: 2020-09-22 08:39

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