Skip to content

[IncludeTree] Support -frewrite-includes with include-tree #8531

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 29 additions & 0 deletions clang/lib/Lex/PPDirectives.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1995,13 +1995,41 @@ void Preprocessor::HandleIncludeDirective(SourceLocation HashLoc,
auto Include = CActions->handleIncludeDirective(
*this, IncludePos, CurLexer->getSourceLocation());

auto InclusionCallback = [&](OptionalFileEntryRef FileRef,
const Module *SuggestedModule) {
if (!Callbacks || HashLoc.isInvalid())
return;

SmallString<128> FilenameBuffer;
StringRef Filename = getSpelling(FilenameTok, FilenameBuffer);
SourceLocation CharEnd = FilenameTok.getEndLoc();
CharSourceRange FilenameRange =
CharSourceRange::getCharRange(FilenameTok.getLocation(), CharEnd);
bool isAngled =
GetIncludeFilenameSpelling(FilenameTok.getLocation(), Filename);
SrcMgr::CharacteristicKind FileCharacter =
SourceMgr.getFileCharacteristic(FilenameTok.getLocation());
if (SuggestedModule)
Callbacks->InclusionDirective(HashLoc, IncludeTok, Filename, isAngled,
FilenameRange, OptionalFileEntryRef(),
/*SearchPath=*/"", /*RelativePath=*/"",
SuggestedModule,
/*ModuleImported=*/true, FileCharacter);
else
Callbacks->InclusionDirective(
HashLoc, IncludeTok, Filename, isAngled, FilenameRange, FileRef,
/*SearchPath=*/"", /*RelativePath=*/"", /*SuggestedModule=*/nullptr,
/*ModuleImported=*/false, FileCharacter);
};

auto HandleIncludeFile = [&](const PPCachedActions::IncludeFile *File) {
const FileEntry *FE = SourceMgr.getFileEntryForID(File->FID);
bool IsImport =
IncludeTok.getIdentifierInfo()->getPPKeywordID() == tok::pp_import;
if (FE && IsImport) {
HeaderInfo.getFileInfo(FE).isImport = true;
}
InclusionCallback(SourceMgr.getFileEntryRefForID(File->FID), nullptr);
EnterSourceFile(File->FID, nullptr, FilenameTok.getLocation(),
/*IsFirstIncludeOfFile*/ true);

Expand Down Expand Up @@ -2068,6 +2096,7 @@ void Preprocessor::HandleIncludeDirective(SourceLocation HashLoc,
return;
}

InclusionCallback({}, Imported);
makeModuleVisible(Imported, EndLoc);
if (IncludeTok.getIdentifierInfo()->getPPKeywordID() !=
tok::pp___include_macros)
Expand Down
84 changes: 84 additions & 0 deletions clang/test/CAS/rewrite-includes.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
// RUN: rm -rf %t
// RUN: split-file %s %t

// RUN: sed -e "s|DIR|%/t|g" %t/cdb.json.template > %t/cdb.json
// RUN: clang-scan-deps -compilation-database %t/cdb.json -format experimental-include-tree-full -cas-path %t/cas > %t/deps.json

// RUN: %deps-to-rsp %t/deps.json --module-name dummy > %t/dummy.rsp
// RUN: %deps-to-rsp %t/deps.json --module-name Mod > %t/mod.rsp
// RUN: %deps-to-rsp %t/deps.json --module-name Spurious > %t/spurious.rsp
// RUN: %deps-to-rsp %t/deps.json --tu-index 0 > %t/tu.rsp
// RUN: %clang @%t/dummy.rsp
// RUN: %clang @%t/mod.rsp
// RUN: %clang @%t/spurious.rsp
// RUN: %clang @%t/tu.rsp -frewrite-includes -w -E -o - | FileCheck %s

// CHECK: int bar();{{$}}
// CHECK-NEXT: #if 0 /* expanded by -frewrite-includes */{{$}}
// CHECK-NEXT: #include "test.h"{{$}}
// CHECK-NEXT: #endif /* expanded by -frewrite-includes */{{$}}
// CHECK-NEXT: # 2 "{{.*[/\\]}}main.c"{{$}}
// CHECK-NEXT: # 1 "{{.*[/\\]}}test.h" 1{{$}}
// CHECK-NEXT: #if 0 /* expanded by -frewrite-includes */{{$}}
// CHECK-NEXT: #include "dummy.h"{{$}}
// CHECK-NEXT: #endif /* expanded by -frewrite-includes */{{$}}
// CHECK-NEXT: # 1 "{{.*[/\\]}}test.h"{{$}}
// CHECK-NEXT: #pragma clang module import dummy /* clang -frewrite-includes: implicit import */{{$}}
// CHECK-NEXT: # 2 "{{.*[/\\]}}test.h"{{$}}
// CHECK-NEXT: # 3 "{{.*[/\\]}}main.c" 2{{$}}
// CHECK-NEXT: int foo();{{$}}
// CHECK-NEXT: #if 0 /* expanded by -frewrite-includes */{{$}}
// CHECK-NEXT: #include "dummy.h"{{$}}
// CHECK-NEXT: #endif /* expanded by -frewrite-includes */{{$}}
// CHECK-NEXT: # 4 "{{.*[/\\]}}main.c"{{$}}
// CHECK-NEXT: #pragma clang module import dummy /* clang -frewrite-includes: implicit import */{{$}}
// CHECK-NEXT: # 5 "{{.*[/\\]}}main.c"{{$}}
// CHECK-NEXT: #if 0 /* expanded by -frewrite-includes */{{$}}
// CHECK-NEXT: #include <Spurious/Missing.h>{{$}}
// CHECK-NEXT: #endif /* expanded by -frewrite-includes */{{$}}
// CHECK-NEXT: # 5 "{{.*[/\\]}}main.c"{{$}}
// CHECK-NEXT: # 1 "{{.*[/\\]}}Missing.h" 1{{$}}
// CHECK-NEXT: /* empty */
// CHECK-NEXT: # 6 "{{.*[/\\]}}main.c" 2{{$}}
// CHECK-NEXT: #if 0 /* expanded by -frewrite-includes */{{$}}
// CHECK-NEXT: #include <Mod.h>{{$}}
// CHECK-NEXT: #endif /* expanded by -frewrite-includes */{{$}}
// CHECK-NEXT: # 6 "{{.*[/\\]}}main.c"{{$}}
// CHECK-NEXT: #pragma clang module import Mod /* clang -frewrite-includes: implicit import */{{$}}
// CHECK-NEXT: # 7 "{{.*[/\\]}}main.c"{{$}}

//--- cdb.json.template
[
{
"directory": "DIR",
"command": "clang -fsyntax-only -fmodules DIR/main.c -F DIR/frameworks -I DIR -fmodules-cache-path=DIR/module-cache",
"file": "DIR/t.c"
}
]

//--- dummy.h
extern int dummy;

//--- module.modulemap
module dummy { header "dummy.h" }
module Mod { header "Mod.h" }
//--- frameworks/Spurious.framework/Modules/module.modulemap
framework module Spurious {
umbrella header "Spurious.h"
module * { export * }
}
//--- frameworks/Spurious.framework/Headers/Spurious.h
#include <Mod.h>
//--- frameworks/Spurious.framework/Headers/Missing.h
/* empty */
//--- Mod.h
typedef int mod_int;
//--- test.h
#include "dummy.h"
//--- main.c
int bar();
#include "test.h"
int foo();
#include "dummy.h"
#include <Spurious/Missing.h>
#include <Mod.h>