Coverage Report

Created: 2020-02-15 09:57

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