@@ -90,8 +90,10 @@ class InclusionRewriter : public PPCallbacks {
90
90
bool EnsureNewline);
91
91
void CommentOutDirective (Lexer &DirectivesLex, const Token &StartToken,
92
92
const MemoryBufferRef &FromFile, StringRef EOL,
93
- unsigned &NextToWrite, int &Lines);
93
+ unsigned &NextToWrite, int &Lines,
94
+ const IncludedFile *Inc = nullptr );
94
95
const IncludedFile *FindIncludeAtLocation (SourceLocation Loc) const ;
96
+ StringRef getIncludedFileName (const IncludedFile *Inc) const ;
95
97
const Module *FindModuleAtLocation (SourceLocation Loc) const ;
96
98
const Module *FindEnteredModule (SourceLocation Loc) const ;
97
99
bool IsIfAtLocationTrue (SourceLocation Loc) const ;
@@ -311,6 +313,17 @@ void InclusionRewriter::OutputContentUpTo(const MemoryBufferRef &FromFile,
311
313
WriteFrom = WriteTo;
312
314
}
313
315
316
+ StringRef
317
+ InclusionRewriter::getIncludedFileName (const IncludedFile *Inc) const {
318
+ if (Inc) {
319
+ auto B = SM.getBufferOrNone (Inc->Id );
320
+ assert (B && " Attempting to process invalid inclusion" );
321
+ if (B)
322
+ return llvm::sys::path::filename (B->getBufferIdentifier ());
323
+ }
324
+ return StringRef ();
325
+ }
326
+
314
327
// / Print characters from \p FromFile starting at \p NextToWrite up until the
315
328
// / inclusion directive at \p StartToken, then print out the inclusion
316
329
// / inclusion directive disabled by a #if directive, updating \p NextToWrite
@@ -320,7 +333,8 @@ void InclusionRewriter::CommentOutDirective(Lexer &DirectiveLex,
320
333
const Token &StartToken,
321
334
const MemoryBufferRef &FromFile,
322
335
StringRef LocalEOL,
323
- unsigned &NextToWrite, int &Line) {
336
+ unsigned &NextToWrite, int &Line,
337
+ const IncludedFile *Inc) {
324
338
OutputContentUpTo (FromFile, NextToWrite,
325
339
SM.getFileOffset (StartToken.getLocation ()), LocalEOL, Line,
326
340
false );
@@ -332,12 +346,21 @@ void InclusionRewriter::CommentOutDirective(Lexer &DirectiveLex,
332
346
// OutputContentUpTo() would not output anything anyway.
333
347
return ;
334
348
}
335
- OS << " #if 0 /* expanded by -frewrite-includes */" << MainEOL;
349
+ if (Inc) {
350
+ OS << " #if defined(__CLANG_REWRITTEN_INCLUDES) " ;
351
+ if (isSystem (Inc->FileType ))
352
+ OS << " || defined(__CLANG_REWRITTEN_SYSTEM_INCLUDES) " ;
353
+ OS << " /* " << getIncludedFileName (Inc);
354
+ } else {
355
+ OS << " #if 0 /*" ;
356
+ }
357
+ OS << " expanded by -frewrite-includes */" << MainEOL;
336
358
OutputContentUpTo (FromFile, NextToWrite,
337
359
SM.getFileOffset (DirectiveToken.getLocation ()) +
338
360
DirectiveToken.getLength (),
339
361
LocalEOL, Line, true );
340
- OS << " #endif /* expanded by -frewrite-includes */" << MainEOL;
362
+ OS << (Inc ? " #else /* " : " #endif /*" ) << getIncludedFileName (Inc)
363
+ << " expanded by -frewrite-includes */" << MainEOL;
341
364
}
342
365
343
366
// / Find the next identifier in the pragma directive specified by \p RawToken.
@@ -400,15 +423,16 @@ void InclusionRewriter::Process(FileID FileId,
400
423
case tok::pp_include:
401
424
case tok::pp_include_next:
402
425
case tok::pp_import: {
403
- CommentOutDirective (RawLex, HashToken, FromFile, LocalEOL, NextToWrite,
404
- Line);
426
+ SourceLocation Loc = HashToken.getLocation ();
427
+ const IncludedFile *Inc = FindIncludeAtLocation (Loc);
428
+ CommentOutDirective (RawLex, HashToken, FromFile, LocalEOL,
429
+ NextToWrite, Line, Inc);
405
430
if (FileId != PP.getPredefinesFileID ())
406
431
WriteLineInfo (FileName, Line - 1 , FileType, " " );
407
432
StringRef LineInfoExtra;
408
- SourceLocation Loc = HashToken.getLocation ();
409
433
if (const Module *Mod = FindModuleAtLocation (Loc))
410
434
WriteImplicitModuleImport (Mod);
411
- else if (const IncludedFile * Inc = FindIncludeAtLocation (Loc) ) {
435
+ else if (Inc) {
412
436
const Module *Mod = FindEnteredModule (Loc);
413
437
if (Mod)
414
438
OS << " #pragma clang module begin "
@@ -420,6 +444,8 @@ void InclusionRewriter::Process(FileID FileId,
420
444
if (Mod)
421
445
OS << " #pragma clang module end /*"
422
446
<< Mod->getFullModuleName (true ) << " */\n " ;
447
+ OS << " #endif /* " << getIncludedFileName (Inc)
448
+ << " expanded by -frewrite-includes */" << LocalEOL;
423
449
424
450
// Add line marker to indicate we're returning from an included
425
451
// file.
0 commit comments