Coverage Report

Created: 2019-07-24 05:18

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