Coverage Report

Created: 2020-09-15 12:33

/Users/buildslave/jenkins/workspace/coverage/llvm-project/clang/lib/Frontend/Rewrite/InclusionRewriter.cpp
Line
Count
Source (jump to first uncovered line)
1
//===--- InclusionRewriter.cpp - Rewrite includes into their expansions ---===//
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 code rewrites include invocations into their expansions.  This gives you
10
// a file with all included files merged into it.
11
//
12
//===----------------------------------------------------------------------===//
13
14
#include "clang/Rewrite/Frontend/Rewriters.h"
15
#include "clang/Basic/SourceManager.h"
16
#include "clang/Frontend/PreprocessorOutputOptions.h"
17
#include "clang/Lex/HeaderSearch.h"
18
#include "clang/Lex/Pragma.h"
19
#include "clang/Lex/Preprocessor.h"
20
#include "llvm/ADT/SmallString.h"
21
#include "llvm/Support/raw_ostream.h"
22
23
using namespace clang;
24
using namespace llvm;
25
26
namespace {
27
28
class InclusionRewriter : public PPCallbacks {
29
  /// Information about which #includes were actually performed,
30
  /// created by preprocessor callbacks.
31
  struct IncludedFile {
32
    FileID Id;
33
    SrcMgr::CharacteristicKind FileType;
34
    const DirectoryLookup *DirLookup;
35
    IncludedFile(FileID Id, SrcMgr::CharacteristicKind FileType,
36
                 const DirectoryLookup *DirLookup)
37
86
        : Id(Id), FileType(FileType), DirLookup(DirLookup) {}
38
  };
39
  Preprocessor &PP; ///< Used to find inclusion directives.
40
  SourceManager &SM; ///< Used to read and manage source files.
41
  raw_ostream &OS; ///< The destination stream for rewritten contents.
42
  StringRef MainEOL; ///< The line ending marker to use.
43
  const llvm::MemoryBuffer *PredefinesBuffer; ///< The preprocessor predefines.
44
  bool ShowLineMarkers; ///< Show #line markers.
45
  bool UseLineDirectives; ///< Use of line directives or line markers.
46
  /// Tracks where inclusions that change the file are found.
47
  std::map<unsigned, IncludedFile> FileIncludes;
48
  /// Tracks where inclusions that import modules are found.
49
  std::map<unsigned, const Module *> ModuleIncludes;
50
  /// Tracks where inclusions that enter modules (in a module build) are found.
51
  std::map<unsigned, const Module *> ModuleEntryIncludes;
52
  /// Tracks where #if and #elif directives get evaluated and whether to true.
53
  std::map<unsigned, bool> IfConditions;
54
  /// Used transitively for building up the FileIncludes mapping over the
55
  /// various \c PPCallbacks callbacks.
56
  SourceLocation LastInclusionLocation;
57
public:
58
  InclusionRewriter(Preprocessor &PP, raw_ostream &OS, bool ShowLineMarkers,
59
                    bool UseLineDirectives);
60
  void Process(FileID FileId, SrcMgr::CharacteristicKind FileType,
61
               const DirectoryLookup *DirLookup);
62
55
  void setPredefinesBuffer(const llvm::MemoryBuffer *Buf) {
63
55
    PredefinesBuffer = Buf;
64
55
  }
65
  void detectMainFileEOL();
66
42
  void handleModuleBegin(Token &Tok) {
67
42
    assert(Tok.getKind() == tok::annot_module_begin);
68
42
    ModuleEntryIncludes.insert({Tok.getLocation().getRawEncoding(),
69
42
                                (Module *)Tok.getAnnotationValue()});
70
42
  }
71
private:
72
  void FileChanged(SourceLocation Loc, FileChangeReason Reason,
73
                   SrcMgr::CharacteristicKind FileType,
74
                   FileID PrevFID) override;
75
  void FileSkipped(const FileEntryRef &SkippedFile, const Token &FilenameTok,
76
                   SrcMgr::CharacteristicKind FileType) override;
77
  void InclusionDirective(SourceLocation HashLoc, const Token &IncludeTok,
78
                          StringRef FileName, bool IsAngled,
79
                          CharSourceRange FilenameRange, const FileEntry *File,
80
                          StringRef SearchPath, StringRef RelativePath,
81
                          const Module *Imported,
82
                          SrcMgr::CharacteristicKind FileType) override;
83
  void If(SourceLocation Loc, SourceRange ConditionRange,
84
          ConditionValueKind ConditionValue) override;
85
  void Elif(SourceLocation Loc, SourceRange ConditionRange,
86
            ConditionValueKind ConditionValue, SourceLocation IfLoc) override;
87
  void WriteLineInfo(StringRef Filename, int Line,
88
                     SrcMgr::CharacteristicKind FileType,
89
                     StringRef Extra = StringRef());
90
  void WriteImplicitModuleImport(const Module *Mod);
91
  void OutputContentUpTo(const MemoryBuffer &FromFile,
92
                         unsigned &WriteFrom, unsigned WriteTo,
93
                         StringRef EOL, int &lines,
94
                         bool EnsureNewline);
95
  void CommentOutDirective(Lexer &DirectivesLex, const Token &StartToken,
96
                           const MemoryBuffer &FromFile, StringRef EOL,
97
                           unsigned &NextToWrite, int &Lines);
98
  const IncludedFile *FindIncludeAtLocation(SourceLocation Loc) const;
99
  const Module *FindModuleAtLocation(SourceLocation Loc) const;
100
  const Module *FindEnteredModule(SourceLocation Loc) const;
101
  bool IsIfAtLocationTrue(SourceLocation Loc) const;
102
  StringRef NextIdentifierName(Lexer &RawLex, Token &RawToken);
103
};
104
105
}  // end anonymous namespace
106
107
/// Initializes an InclusionRewriter with a \p PP source and \p OS destination.
108
InclusionRewriter::InclusionRewriter(Preprocessor &PP, raw_ostream &OS,
109
                                     bool ShowLineMarkers,
110
                                     bool UseLineDirectives)
111
    : PP(PP), SM(PP.getSourceManager()), OS(OS), MainEOL("\n"),
112
      PredefinesBuffer(nullptr), ShowLineMarkers(ShowLineMarkers),
113
      UseLineDirectives(UseLineDirectives),
114
55
      LastInclusionLocation(SourceLocation()) {}
115
116
/// Write appropriate line information as either #line directives or GNU line
117
/// markers depending on what mode we're in, including the \p Filename and
118
/// \p Line we are located at, using the specified \p EOL line separator, and
119
/// any \p Extra context specifiers in GNU line directives.
120
void InclusionRewriter::WriteLineInfo(StringRef Filename, int Line,
121
                                      SrcMgr::CharacteristicKind FileType,
122
568
                                      StringRef Extra) {
123
568
  if (!ShowLineMarkers)
124
47
    return;
125
521
  if (UseLineDirectives) {
126
11
    OS << "#line" << ' ' << Line << ' ' << '"';
127
11
    OS.write_escaped(Filename);
128
11
    OS << '"';
129
510
  } else {
130
    // Use GNU linemarkers as described here:
131
    // http://gcc.gnu.org/onlinedocs/cpp/Preprocessor-Output.html
132
510
    OS << '#' << ' ' << Line << ' ' << '"';
133
510
    OS.write_escaped(Filename);
134
510
    OS << '"';
135
510
    if (!Extra.empty())
136
148
      OS << Extra;
137
510
    if (FileType == SrcMgr::C_System)
138
      // "`3' This indicates that the following text comes from a system header
139
      // file, so certain warnings should be suppressed."
140
8
      OS << " 3";
141
502
    else if (FileType == SrcMgr::C_ExternCSystem)
142
      // as above for `3', plus "`4' This indicates that the following text
143
      // should be treated as being wrapped in an implicit extern "C" block."
144
0
      OS << " 3 4";
145
510
  }
146
521
  OS << MainEOL;
147
521
}
148
149
21
void InclusionRewriter::WriteImplicitModuleImport(const Module *Mod) {
150
21
  OS << "#pragma clang module import " << Mod->getFullModuleName(true)
151
21
     << " /* clang -frewrite-includes: implicit import */" << MainEOL;
152
21
}
153
154
/// FileChanged - Whenever the preprocessor enters or exits a #include file
155
/// it invokes this handler.
156
void InclusionRewriter::FileChanged(SourceLocation Loc,
157
                                    FileChangeReason Reason,
158
                                    SrcMgr::CharacteristicKind NewFileType,
159
505
                                    FileID) {
160
505
  if (Reason != EnterFile)
161
254
    return;
162
251
  if (LastInclusionLocation.isInvalid())
163
    // we didn't reach this file (eg: the main file) via an inclusion directive
164
165
    return;
165
86
  FileID Id = FullSourceLoc(Loc, SM).getFileID();
166
86
  auto P = FileIncludes.insert(
167
86
      std::make_pair(LastInclusionLocation.getRawEncoding(),
168
86
                     IncludedFile(Id, NewFileType, PP.GetCurDirLookup())));
169
86
  (void)P;
170
86
  assert(P.second && "Unexpected revisitation of the same include directive");
171
86
  LastInclusionLocation = SourceLocation();
172
86
}
173
174
/// Called whenever an inclusion is skipped due to canonical header protection
175
/// macros.
176
void InclusionRewriter::FileSkipped(const FileEntryRef & /*SkippedFile*/,
177
                                    const Token & /*FilenameTok*/,
178
7
                                    SrcMgr::CharacteristicKind /*FileType*/) {
179
7
  assert(LastInclusionLocation.isValid() &&
180
7
         "A file, that wasn't found via an inclusion directive, was skipped");
181
7
  LastInclusionLocation = SourceLocation();
182
7
}
183
184
/// This should be called whenever the preprocessor encounters include
185
/// directives. It does not say whether the file has been included, but it
186
/// provides more information about the directive (hash location instead
187
/// of location inside the included file). It is assumed that the matching
188
/// FileChanged() or FileSkipped() is called after this (or neither is
189
/// called if this #include results in an error or does not textually include
190
/// anything).
191
void InclusionRewriter::InclusionDirective(SourceLocation HashLoc,
192
                                           const Token &/*IncludeTok*/,
193
                                           StringRef /*FileName*/,
194
                                           bool /*IsAngled*/,
195
                                           CharSourceRange /*FilenameRange*/,
196
                                           const FileEntry * /*File*/,
197
                                           StringRef /*SearchPath*/,
198
                                           StringRef /*RelativePath*/,
199
                                           const Module *Imported,
200
117
                                           SrcMgr::CharacteristicKind FileType){
201
117
  if (Imported) {
202
21
    auto P = ModuleIncludes.insert(
203
21
        std::make_pair(HashLoc.getRawEncoding(), Imported));
204
21
    (void)P;
205
21
    assert(P.second && "Unexpected revisitation of the same include directive");
206
21
  } else
207
96
    LastInclusionLocation = HashLoc;
208
117
}
209
210
void InclusionRewriter::If(SourceLocation Loc, SourceRange ConditionRange,
211
25
                           ConditionValueKind ConditionValue) {
212
25
  auto P = IfConditions.insert(
213
25
      std::make_pair(Loc.getRawEncoding(), ConditionValue == CVK_True));
214
25
  (void)P;
215
25
  assert(P.second && "Unexpected revisitation of the same if directive");
216
25
}
217
218
void InclusionRewriter::Elif(SourceLocation Loc, SourceRange ConditionRange,
219
                             ConditionValueKind ConditionValue,
220
17
                             SourceLocation IfLoc) {
221
17
  auto P = IfConditions.insert(
222
17
      std::make_pair(Loc.getRawEncoding(), ConditionValue == CVK_True));
223
17
  (void)P;
224
17
  assert(P.second && "Unexpected revisitation of the same elif directive");
225
17
}
226
227
/// Simple lookup for a SourceLocation (specifically one denoting the hash in
228
/// an inclusion directive) in the map of inclusion information, FileChanges.
229
const InclusionRewriter::IncludedFile *
230
107
InclusionRewriter::FindIncludeAtLocation(SourceLocation Loc) const {
231
107
  const auto I = FileIncludes.find(Loc.getRawEncoding());
232
107
  if (I != FileIncludes.end())
233
86
    return &I->second;
234
21
  return nullptr;
235
21
}
236
237
/// Simple lookup for a SourceLocation (specifically one denoting the hash in
238
/// an inclusion directive) in the map of module inclusion information.
239
const Module *
240
128
InclusionRewriter::FindModuleAtLocation(SourceLocation Loc) const {
241
128
  const auto I = ModuleIncludes.find(Loc.getRawEncoding());
242
128
  if (I != ModuleIncludes.end())
243
21
    return I->second;
244
107
  return nullptr;
245
107
}
246
247
/// Simple lookup for a SourceLocation (specifically one denoting the hash in
248
/// an inclusion directive) in the map of module entry information.
249
const Module *
250
86
InclusionRewriter::FindEnteredModule(SourceLocation Loc) const {
251
86
  const auto I = ModuleEntryIncludes.find(Loc.getRawEncoding());
252
86
  if (I != ModuleEntryIncludes.end())
253
42
    return I->second;
254
44
  return nullptr;
255
44
}
256
257
44
bool InclusionRewriter::IsIfAtLocationTrue(SourceLocation Loc) const {
258
44
  const auto I = IfConditions.find(Loc.getRawEncoding());
259
44
  if (I != IfConditions.end())
260
42
    return I->second;
261
2
  return false;
262
2
}
263
264
/// Detect the likely line ending style of \p FromFile by examining the first
265
/// newline found within it.
266
251
static StringRef DetectEOL(const MemoryBuffer &FromFile) {
267
  // Detect what line endings the file uses, so that added content does not mix
268
  // the style. We need to check for "\r\n" first because "\n\r" will match
269
  // "\r\n\r\n".
270
251
  const char *Pos = strchr(FromFile.getBufferStart(), '\n');
271
251
  if (!Pos)
272
2
    return "\n";
273
249
  if (Pos - 1 >= FromFile.getBufferStart() && Pos[-1] == '\r')
274
0
    return "\r\n";
275
249
  if (Pos + 1 < FromFile.getBufferEnd() && 
Pos[1] == '\r'194
)
276
0
    return "\n\r";
277
249
  return "\n";
278
249
}
279
280
55
void InclusionRewriter::detectMainFileEOL() {
281
55
  bool Invalid;
282
55
  const MemoryBuffer &FromFile = *SM.getBuffer(SM.getMainFileID(), &Invalid);
283
55
  assert(!Invalid);
284
55
  if (Invalid)
285
0
    return; // Should never happen, but whatever.
286
55
  MainEOL = DetectEOL(FromFile);
287
55
}
288
289
/// Writes out bytes from \p FromFile, starting at \p NextToWrite and ending at
290
/// \p WriteTo - 1.
291
void InclusionRewriter::OutputContentUpTo(const MemoryBuffer &FromFile,
292
                                          unsigned &WriteFrom, unsigned WriteTo,
293
                                          StringRef LocalEOL, int &Line,
294
616
                                          bool EnsureNewline) {
295
616
  if (WriteTo <= WriteFrom)
296
147
    return;
297
469
  if (&FromFile == PredefinesBuffer) {
298
    // Ignore the #defines of the predefines buffer.
299
57
    WriteFrom = WriteTo;
300
57
    return;
301
57
  }
302
412
303
  // If we would output half of a line ending, advance one character to output
304
  // the whole line ending.  All buffers are null terminated, so looking ahead
305
  // one byte is safe.
306
412
  if (LocalEOL.size() == 2 &&
307
0
      LocalEOL[0] == (FromFile.getBufferStart() + WriteTo)[-1] &&
308
0
      LocalEOL[1] == (FromFile.getBufferStart() + WriteTo)[0])
309
0
    WriteTo++;
310
412
311
412
  StringRef TextToWrite(FromFile.getBufferStart() + WriteFrom,
312
412
                        WriteTo - WriteFrom);
313
412
314
412
  if (MainEOL == LocalEOL) {
315
412
    OS << TextToWrite;
316
    // count lines manually, it's faster than getPresumedLoc()
317
412
    Line += TextToWrite.count(LocalEOL);
318
412
    if (EnsureNewline && 
!TextToWrite.endswith(LocalEOL)363
)
319
1
      OS << MainEOL;
320
0
  } else {
321
    // Output the file one line at a time, rewriting the line endings as we go.
322
0
    StringRef Rest = TextToWrite;
323
0
    while (!Rest.empty()) {
324
0
      StringRef LineText;
325
0
      std::tie(LineText, Rest) = Rest.split(LocalEOL);
326
0
      OS << LineText;
327
0
      Line++;
328
0
      if (!Rest.empty())
329
0
        OS << MainEOL;
330
0
    }
331
0
    if (TextToWrite.endswith(LocalEOL) || EnsureNewline)
332
0
      OS << MainEOL;
333
0
  }
334
412
  WriteFrom = WriteTo;
335
412
}
336
337
/// Print characters from \p FromFile starting at \p NextToWrite up until the
338
/// inclusion directive at \p StartToken, then print out the inclusion
339
/// inclusion directive disabled by a #if directive, updating \p NextToWrite
340
/// and \p Line to track the number of source lines visited and the progress
341
/// through the \p FromFile buffer.
342
void InclusionRewriter::CommentOutDirective(Lexer &DirectiveLex,
343
                                            const Token &StartToken,
344
                                            const MemoryBuffer &FromFile,
345
                                            StringRef LocalEOL,
346
134
                                            unsigned &NextToWrite, int &Line) {
347
134
  OutputContentUpTo(FromFile, NextToWrite,
348
134
                    SM.getFileOffset(StartToken.getLocation()), LocalEOL, Line,
349
134
                    false);
350
134
  Token DirectiveToken;
351
292
  do {
352
292
    DirectiveLex.LexFromRawLexer(DirectiveToken);
353
292
  } while (!DirectiveToken.is(tok::eod) && 
DirectiveToken.isNot(tok::eof)158
);
354
134
  if (&FromFile == PredefinesBuffer) {
355
    // OutputContentUpTo() would not output anything anyway.
356
2
    return;
357
2
  }
358
132
  OS << "#if 0 /* expanded by -frewrite-includes */" << MainEOL;
359
132
  OutputContentUpTo(FromFile, NextToWrite,
360
132
                    SM.getFileOffset(DirectiveToken.getLocation()) +
361
132
                        DirectiveToken.getLength(),
362
132
                    LocalEOL, Line, true);
363
132
  OS << "#endif /* expanded by -frewrite-includes */" << MainEOL;
364
132
}
365
366
/// Find the next identifier in the pragma directive specified by \p RawToken.
367
StringRef InclusionRewriter::NextIdentifierName(Lexer &RawLex,
368
57
                                                Token &RawToken) {
369
57
  RawLex.LexFromRawLexer(RawToken);
370
57
  if (RawToken.is(tok::raw_identifier))
371
57
    PP.LookUpIdentifierInfo(RawToken);
372
57
  if (RawToken.is(tok::identifier))
373
57
    return RawToken.getIdentifierInfo()->getName();
374
0
  return StringRef();
375
0
}
376
377
/// Use a raw lexer to analyze \p FileId, incrementally copying parts of it
378
/// and including content of included files recursively.
379
void InclusionRewriter::Process(FileID FileId,
380
                                SrcMgr::CharacteristicKind FileType,
381
196
                                const DirectoryLookup *DirLookup) {
382
196
  bool Invalid;
383
196
  const MemoryBuffer &FromFile = *SM.getBuffer(FileId, &Invalid);
384
196
  assert(!Invalid && "Attempting to process invalid inclusion");
385
196
  StringRef FileName = FromFile.getBufferIdentifier();
386
196
  Lexer RawLex(FileId, &FromFile, PP.getSourceManager(), PP.getLangOpts());
387
196
  RawLex.SetCommentRetentionState(false);
388
196
389
196
  StringRef LocalEOL = DetectEOL(FromFile);
390
196
391
  // Per the GNU docs: "1" indicates entering a new file.
392
196
  if (FileId == SM.getMainFileID() || 
FileId == PP.getPredefinesFileID()141
)
393
110
    WriteLineInfo(FileName, 1, FileType, "");
394
86
  else
395
86
    WriteLineInfo(FileName, 1, FileType, " 1");
396
196
397
196
  if (SM.getFileIDSize(FileId) == 0)
398
2
    return;
399
194
400
  // The next byte to be copied from the source file, which may be non-zero if
401
  // the lexer handled a BOM.
402
194
  unsigned NextToWrite = SM.getFileOffset(RawLex.getSourceLocation());
403
194
  assert(SM.getLineNumber(FileId, NextToWrite) == 1);
404
194
  int Line = 1; // The current input file line number.
405
194
406
194
  Token RawToken;
407
194
  RawLex.LexFromRawLexer(RawToken);
408
194
409
  // TODO: Consider adding a switch that strips possibly unimportant content,
410
  // such as comments, to reduce the size of repro files.
411
62.5k
  while (RawToken.isNot(tok::eof)) {
412
62.3k
    if (RawToken.is(tok::hash) && 
RawToken.isAtStartOfLine()19.2k
) {
413
19.2k
      RawLex.setParsingPreprocessorDirective(true);
414
19.2k
      Token HashToken = RawToken;
415
19.2k
      RawLex.LexFromRawLexer(RawToken);
416
19.2k
      if (RawToken.is(tok::raw_identifier))
417
19.0k
        PP.LookUpIdentifierInfo(RawToken);
418
19.2k
      if (RawToken.getIdentifierInfo() != nullptr) {
419
19.0k
        switch (RawToken.getIdentifierInfo()->getPPKeywordID()) {
420
128
          case tok::pp_include:
421
128
          case tok::pp_include_next:
422
128
          case tok::pp_import: {
423
128
            CommentOutDirective(RawLex, HashToken, FromFile, LocalEOL, NextToWrite,
424
128
              Line);
425
128
            if (FileId != PP.getPredefinesFileID())
426
126
              WriteLineInfo(FileName, Line - 1, FileType, "");
427
128
            StringRef LineInfoExtra;
428
128
            SourceLocation Loc = HashToken.getLocation();
429
128
            if (const Module *Mod = FindModuleAtLocation(Loc))
430
21
              WriteImplicitModuleImport(Mod);
431
107
            else if (const IncludedFile *Inc = FindIncludeAtLocation(Loc)) {
432
86
              const Module *Mod = FindEnteredModule(Loc);
433
86
              if (Mod)
434
42
                OS << "#pragma clang module begin "
435
42
                   << Mod->getFullModuleName(true) << "\n";
436
86
437
              // Include and recursively process the file.
438
86
              Process(Inc->Id, Inc->FileType, Inc->DirLookup);
439
86
440
86
              if (Mod)
441
42
                OS << "#pragma clang module end /*"
442
42
                   << Mod->getFullModuleName(true) << "*/\n";
443
86
444
              // Add line marker to indicate we're returning from an included
445
              // file.
446
86
              LineInfoExtra = " 2";
447
86
            }
448
            // fix up lineinfo (since commented out directive changed line
449
            // numbers) for inclusions that were skipped due to header guards
450
128
            WriteLineInfo(FileName, Line, FileType, LineInfoExtra);
451
128
            break;
452
128
          }
453
31
          case tok::pp_pragma: {
454
31
            StringRef Identifier = NextIdentifierName(RawLex, RawToken);
455
31
            if (Identifier == "clang" || 
Identifier == "GCC"7
) {
456
26
              if (NextIdentifierName(RawLex, RawToken) == "system_header") {
457
                // keep the directive in, commented out
458
3
                CommentOutDirective(RawLex, HashToken, FromFile, LocalEOL,
459
3
                  NextToWrite, Line);
460
                // update our own type
461
3
                FileType = SM.getFileCharacteristic(RawToken.getLocation());
462
3
                WriteLineInfo(FileName, Line, FileType);
463
3
              }
464
5
            } else if (Identifier == "once") {
465
              // keep the directive in, commented out
466
3
              CommentOutDirective(RawLex, HashToken, FromFile, LocalEOL,
467
3
                NextToWrite, Line);
468
3
              WriteLineInfo(FileName, Line, FileType);
469
3
            }
470
31
            break;
471
128
          }
472
44
          case tok::pp_if:
473
44
          case tok::pp_elif: {
474
44
            bool elif = (RawToken.getIdentifierInfo()->getPPKeywordID() ==
475
44
                         tok::pp_elif);
476
44
            bool isTrue = IsIfAtLocationTrue(RawToken.getLocation());
477
44
            OutputContentUpTo(FromFile, NextToWrite,
478
44
                              SM.getFileOffset(HashToken.getLocation()),
479
44
                              LocalEOL, Line, /*EnsureNewline=*/true);
480
258
            do {
481
258
              RawLex.LexFromRawLexer(RawToken);
482
258
            } while (!RawToken.is(tok::eod) && 
RawToken.isNot(tok::eof)214
);
483
            // We need to disable the old condition, but that is tricky.
484
            // Trying to comment it out can easily lead to comment nesting.
485
            // So instead make the condition harmless by making it enclose
486
            // and empty block. Moreover, put it itself inside an #if 0 block
487
            // to disable it from getting evaluated (e.g. __has_include_next
488
            // warns if used from the primary source file).
489
44
            OS << "#if 0 /* disabled by -frewrite-includes */" << MainEOL;
490
44
            if (elif) {
491
19
              OS << "#if 0" << MainEOL;
492
19
            }
493
44
            OutputContentUpTo(FromFile, NextToWrite,
494
44
                              SM.getFileOffset(RawToken.getLocation()) +
495
44
                                  RawToken.getLength(),
496
44
                              LocalEOL, Line, /*EnsureNewline=*/true);
497
            // Close the empty block and the disabling block.
498
44
            OS << "#endif" << MainEOL;
499
44
            OS << "#endif /* disabled by -frewrite-includes */" << MainEOL;
500
25
            OS << (elif ? 
"#elif "19
: "#if ") << (isTrue ?
"1"19
: "0")
501
44
               << " /* evaluated by -frewrite-includes */" << MainEOL;
502
44
            WriteLineInfo(FileName, Line, FileType);
503
44
            break;
504
44
          }
505
68
          case tok::pp_endif:
506
68
          case tok::pp_else: {
507
            // We surround every #include by #if 0 to comment it out, but that
508
            // changes line numbers. These are fixed up right after that, but
509
            // the whole #include could be inside a preprocessor conditional
510
            // that is not processed. So it is necessary to fix the line
511
            // numbers one the next line after each #else/#endif as well.
512
68
            RawLex.SetKeepWhitespaceMode(true);
513
68
            do {
514
68
              RawLex.LexFromRawLexer(RawToken);
515
68
            } while (RawToken.isNot(tok::eod) && 
RawToken.isNot(tok::eof)0
);
516
68
            OutputContentUpTo(FromFile, NextToWrite,
517
68
                              SM.getFileOffset(RawToken.getLocation()) +
518
68
                                  RawToken.getLength(),
519
68
                              LocalEOL, Line, /*EnsureNewline=*/ true);
520
68
            WriteLineInfo(FileName, Line, FileType);
521
68
            RawLex.SetKeepWhitespaceMode(false);
522
68
            break;
523
68
          }
524
18.8k
          default:
525
18.8k
            break;
526
19.2k
        }
527
19.2k
      }
528
19.2k
      RawLex.setParsingPreprocessorDirective(false);
529
19.2k
    }
530
62.3k
    RawLex.LexFromRawLexer(RawToken);
531
62.3k
  }
532
194
  OutputContentUpTo(FromFile, NextToWrite,
533
194
                    SM.getFileOffset(SM.getLocForEndOfFile(FileId)), LocalEOL,
534
194
                    Line, /*EnsureNewline=*/true);
535
194
}
536
537
/// InclusionRewriterInInput - Implement -frewrite-includes mode.
538
void clang::RewriteIncludesInInput(Preprocessor &PP, raw_ostream *OS,
539
55
                                   const PreprocessorOutputOptions &Opts) {
540
55
  SourceManager &SM = PP.getSourceManager();
541
55
  InclusionRewriter *Rewrite = new InclusionRewriter(
542
55
      PP, *OS, Opts.ShowLineMarkers, Opts.UseLineDirectives);
543
55
  Rewrite->detectMainFileEOL();
544
55
545
55
  PP.addPPCallbacks(std::unique_ptr<PPCallbacks>(Rewrite));
546
55
  PP.IgnorePragmas();
547
55
548
  // First let the preprocessor process the entire file and call callbacks.
549
  // Callbacks will record which #include's were actually performed.
550
55
  PP.EnterMainSourceFile();
551
55
  Token Tok;
552
  // Only preprocessor directives matter here, so disable macro expansion
553
  // everywhere else as an optimization.
554
  // TODO: It would be even faster if the preprocessor could be switched
555
  // to a mode where it would parse only preprocessor directives and comments,
556
  // nothing else matters for parsing or processing.
557
55
  PP.SetMacroExpansionOnlyInDirectives();
558
783
  do {
559
783
    PP.Lex(Tok);
560
783
    if (Tok.is(tok::annot_module_begin))
561
42
      Rewrite->handleModuleBegin(Tok);
562
783
  } while (Tok.isNot(tok::eof));
563
55
  Rewrite->setPredefinesBuffer(SM.getBuffer(PP.getPredefinesFileID()));
564
55
  Rewrite->Process(PP.getPredefinesFileID(), SrcMgr::C_User, nullptr);
565
55
  Rewrite->Process(SM.getMainFileID(), SrcMgr::C_User, nullptr);
566
55
  OS->flush();
567
55
}