#include "clang/Frontend/DependencyOutputOptions.h"
#include "clang/Frontend/Utils.h"
#include "clang/Basic/SourceManager.h"
#include "clang/Frontend/FrontendDiagnostic.h"
#include "clang/Lex/Preprocessor.h"
#include "llvm/ADT/SmallString.h"
#include "llvm/Support/JSON.h"
#include "llvm/Support/raw_ostream.h"
using namespace clang;
namespace {
class HeaderIncludesCallback : public PPCallbacks {
SourceManager &SM;
raw_ostream *OutputFile;
const DependencyOutputOptions &DepOpts;
unsigned CurrentIncludeDepth;
bool HasProcessedPredefines;
bool OwnsOutputFile;
bool ShowAllHeaders;
bool ShowDepth;
bool MSStyle;
public:
HeaderIncludesCallback(const Preprocessor *PP, bool ShowAllHeaders_,
raw_ostream *OutputFile_,
const DependencyOutputOptions &DepOpts,
bool OwnsOutputFile_, bool ShowDepth_, bool MSStyle_)
: SM(PP->getSourceManager()), OutputFile(OutputFile_), DepOpts(DepOpts),
CurrentIncludeDepth(0), HasProcessedPredefines(false),
OwnsOutputFile(OwnsOutputFile_), ShowAllHeaders(ShowAllHeaders_),
ShowDepth(ShowDepth_), MSStyle(MSStyle_) {}
~HeaderIncludesCallback() override {
if (OwnsOutputFile)
delete OutputFile;
}
HeaderIncludesCallback(const HeaderIncludesCallback &) = delete;
HeaderIncludesCallback &operator=(const HeaderIncludesCallback &) = delete;
void FileChanged(SourceLocation Loc, FileChangeReason Reason,
SrcMgr::CharacteristicKind FileType,
FileID PrevFID) override;
void FileSkipped(const FileEntryRef &SkippedFile, const Token &FilenameTok,
SrcMgr::CharacteristicKind FileType) override;
private:
bool ShouldShowHeader(SrcMgr::CharacteristicKind HeaderType) {
if (!DepOpts.IncludeSystemHeaders && isSystem(HeaderType))
return false;
return (HasProcessedPredefines ||
(ShowAllHeaders && CurrentIncludeDepth > 2));
}
};
class HeaderIncludesJSONCallback : public PPCallbacks {
SourceManager &SM;
raw_ostream *OutputFile;
bool OwnsOutputFile;
SmallVector<std::string, 16> IncludedHeaders;
public:
HeaderIncludesJSONCallback(const Preprocessor *PP, raw_ostream *OutputFile_,
bool OwnsOutputFile_)
: SM(PP->getSourceManager()), OutputFile(OutputFile_),
OwnsOutputFile(OwnsOutputFile_) {}
~HeaderIncludesJSONCallback() override {
if (OwnsOutputFile)
delete OutputFile;
}
HeaderIncludesJSONCallback(const HeaderIncludesJSONCallback &) = delete;
HeaderIncludesJSONCallback &
operator=(const HeaderIncludesJSONCallback &) = delete;
void EndOfMainFile() override;
void FileChanged(SourceLocation Loc, FileChangeReason Reason,
SrcMgr::CharacteristicKind FileType,
FileID PrevFID) override;
void FileSkipped(const FileEntryRef &SkippedFile, const Token &FilenameTok,
SrcMgr::CharacteristicKind FileType) override;
};
}
static void PrintHeaderInfo(raw_ostream *OutputFile, StringRef Filename,
bool ShowDepth, unsigned CurrentIncludeDepth,
bool MSStyle) {
SmallString<512> Pathname(Filename);
if (!MSStyle)
Lexer::Stringify(Pathname);
SmallString<256> Msg;
if (MSStyle)
Msg += "Note: including file:";
if (ShowDepth) {
for (unsigned i = 1; i != CurrentIncludeDepth; ++i)
Msg += MSStyle ? ' ' : '.';
if (!MSStyle)
Msg += ' ';
}
Msg += Pathname;
Msg += '\n';
*OutputFile << Msg;
OutputFile->flush();
}
void clang::AttachHeaderIncludeGen(Preprocessor &PP,
const DependencyOutputOptions &DepOpts,
bool ShowAllHeaders, StringRef OutputPath,
bool ShowDepth, bool MSStyle) {
raw_ostream *OutputFile = &llvm::errs();
bool OwnsOutputFile = false;
if (MSStyle) {
switch (DepOpts.ShowIncludesDest) {
default:
llvm_unreachable("Invalid destination for /showIncludes output!");
case ShowIncludesDestination::Stderr:
OutputFile = &llvm::errs();
break;
case ShowIncludesDestination::Stdout:
OutputFile = &llvm::outs();
break;
}
}
if (!OutputPath.empty()) {
std::error_code EC;
llvm::raw_fd_ostream *OS = new llvm::raw_fd_ostream(
OutputPath.str(), EC,
llvm::sys::fs::OF_Append | llvm::sys::fs::OF_TextWithCRLF);
if (EC) {
PP.getDiagnostics().Report(clang::diag::warn_fe_cc_print_header_failure)
<< EC.message();
delete OS;
} else {
OS->SetUnbuffered();
OutputFile = OS;
OwnsOutputFile = true;
}
}
switch (DepOpts.HeaderIncludeFormat) {
case HIFMT_None:
llvm_unreachable("unexpected header format kind");
case HIFMT_Textual: {
assert(DepOpts.HeaderIncludeFiltering == HIFIL_None &&
"header filtering is currently always disabled when output format is"
"textual");
for (const auto &Header : DepOpts.ExtraDeps)
PrintHeaderInfo(OutputFile, Header.first, ShowDepth, 2, MSStyle);
PP.addPPCallbacks(std::make_unique<HeaderIncludesCallback>(
&PP, ShowAllHeaders, OutputFile, DepOpts, OwnsOutputFile, ShowDepth,
MSStyle));
break;
}
case HIFMT_JSON: {
assert(DepOpts.HeaderIncludeFiltering == HIFIL_Only_Direct_System &&
"only-direct-system is the only option for filtering");
PP.addPPCallbacks(std::make_unique<HeaderIncludesJSONCallback>(
&PP, OutputFile, OwnsOutputFile));
break;
}
}
}
void HeaderIncludesCallback::FileChanged(SourceLocation Loc,
FileChangeReason Reason,
SrcMgr::CharacteristicKind NewFileType,
FileID PrevFID) {
PresumedLoc UserLoc = SM.getPresumedLoc(Loc);
if (UserLoc.isInvalid())
return;
if (Reason == PPCallbacks::EnterFile) {
++CurrentIncludeDepth;
} else if (Reason == PPCallbacks::ExitFile) {
if (CurrentIncludeDepth)
--CurrentIncludeDepth;
if (CurrentIncludeDepth == 1 && !HasProcessedPredefines)
HasProcessedPredefines = true;
return;
} else {
return;
}
if (!ShouldShowHeader(NewFileType))
return;
unsigned IncludeDepth = CurrentIncludeDepth;
if (!HasProcessedPredefines)
--IncludeDepth;
if (Reason == PPCallbacks::EnterFile &&
UserLoc.getFilename() != StringRef("<command line>")) {
PrintHeaderInfo(OutputFile, UserLoc.getFilename(), ShowDepth, IncludeDepth,
MSStyle);
}
}
void HeaderIncludesCallback::FileSkipped(const FileEntryRef &SkippedFile, const
Token &FilenameTok,
SrcMgr::CharacteristicKind FileType) {
if (!DepOpts.ShowSkippedHeaderIncludes)
return;
if (!ShouldShowHeader(FileType))
return;
PrintHeaderInfo(OutputFile, SkippedFile.getName(), ShowDepth,
CurrentIncludeDepth + 1, MSStyle);
}
void HeaderIncludesJSONCallback::EndOfMainFile() {
OptionalFileEntryRef FE = SM.getFileEntryRefForID(SM.getMainFileID());
SmallString<256> MainFile(FE->getName());
SM.getFileManager().makeAbsolutePath(MainFile);
std::string Str;
llvm::raw_string_ostream OS(Str);
llvm::json::OStream JOS(OS);
JOS.object([&] {
JOS.attribute("source", MainFile.c_str());
JOS.attributeArray("includes", [&] {
llvm::StringSet<> SeenHeaders;
for (const std::string &H : IncludedHeaders)
if (SeenHeaders.insert(H).second)
JOS.value(H);
});
});
OS << "\n";
if (OutputFile->get_kind() == raw_ostream::OStreamKind::OK_FDStream) {
llvm::raw_fd_ostream *FDS = static_cast<llvm::raw_fd_ostream *>(OutputFile);
if (auto L = FDS->lock())
*OutputFile << Str;
} else
*OutputFile << Str;
}
static bool shouldRecordNewFile(SrcMgr::CharacteristicKind NewFileType,
SourceLocation PrevLoc, SourceManager &SM) {
return SrcMgr::isSystem(NewFileType) && !SM.isInSystemHeader(PrevLoc);
}
void HeaderIncludesJSONCallback::FileChanged(
SourceLocation Loc, FileChangeReason Reason,
SrcMgr::CharacteristicKind NewFileType, FileID PrevFID) {
if (PrevFID.isInvalid() ||
!shouldRecordNewFile(NewFileType, SM.getLocForStartOfFile(PrevFID), SM))
return;
PresumedLoc UserLoc = SM.getPresumedLoc(Loc);
if (UserLoc.isInvalid())
return;
if (Reason == PPCallbacks::EnterFile &&
UserLoc.getFilename() != StringRef("<command line>"))
IncludedHeaders.push_back(UserLoc.getFilename());
}
void HeaderIncludesJSONCallback::FileSkipped(
const FileEntryRef &SkippedFile, const Token &FilenameTok,
SrcMgr::CharacteristicKind FileType) {
if (!shouldRecordNewFile(FileType, FilenameTok.getLocation(), SM))
return;
IncludedHeaders.push_back(SkippedFile.getName().str());
}