#include "clang/Basic/CharInfo.h"
#include "clang/Frontend/Utils.h"
#include "clang/Lex/Preprocessor.h"
#include "clang/Serialization/ASTReader.h"
#include "llvm/ADT/iterator_range.h"
#include "llvm/Config/llvm-config.h"
#include "llvm/Support/FileSystem.h"
#include "llvm/Support/Path.h"
#include "llvm/Support/raw_ostream.h"
using namespace clang;
namespace {
class ModuleDependencyListener : public ASTReaderListener {
ModuleDependencyCollector &Collector;
FileManager &FileMgr;
public:
ModuleDependencyListener(ModuleDependencyCollector &Collector,
FileManager &FileMgr)
: Collector(Collector), FileMgr(FileMgr) {}
bool needsInputFileVisitation() override { return true; }
bool needsSystemInputFileVisitation() override { return true; }
bool visitInputFile(StringRef Filename, bool IsSystem, bool IsOverridden,
bool IsExplicitModule) override {
if (auto FE = FileMgr.getOptionalFileRef(Filename))
Filename = FE->getName();
Collector.addFile(Filename);
return true;
}
};
struct ModuleDependencyPPCallbacks : public PPCallbacks {
ModuleDependencyCollector &Collector;
SourceManager &SM;
ModuleDependencyPPCallbacks(ModuleDependencyCollector &Collector,
SourceManager &SM)
: Collector(Collector), SM(SM) {}
void InclusionDirective(SourceLocation HashLoc, const Token &IncludeTok,
StringRef FileName, bool IsAngled,
CharSourceRange FilenameRange,
OptionalFileEntryRef File, StringRef SearchPath,
StringRef RelativePath, const Module *SuggestedModule,
bool ModuleImported,
SrcMgr::CharacteristicKind FileType) override {
if (!File)
return;
Collector.addFile(File->getName());
}
};
struct ModuleDependencyMMCallbacks : public ModuleMapCallbacks {
ModuleDependencyCollector &Collector;
ModuleDependencyMMCallbacks(ModuleDependencyCollector &Collector)
: Collector(Collector) {}
void moduleMapAddHeader(StringRef HeaderPath) override {
if (llvm::sys::path::is_absolute(HeaderPath))
Collector.addFile(HeaderPath);
}
void moduleMapAddUmbrellaHeader(FileEntryRef Header) override {
moduleMapAddHeader(Header.getNameAsRequested());
}
};
}
void ModuleDependencyCollector::attachToASTReader(ASTReader &R) {
R.addListener(
std::make_unique<ModuleDependencyListener>(*this, R.getFileManager()));
}
void ModuleDependencyCollector::attachToPreprocessor(Preprocessor &PP) {
PP.addPPCallbacks(std::make_unique<ModuleDependencyPPCallbacks>(
*this, PP.getSourceManager()));
PP.getHeaderSearchInfo().getModuleMap().addModuleMapCallbacks(
std::make_unique<ModuleDependencyMMCallbacks>(*this));
}
static bool isCaseSensitivePath(StringRef Path) {
SmallString<256> TmpDest = Path, UpperDest, RealDest;
if (llvm::sys::fs::real_path(Path, TmpDest))
return true;
Path = TmpDest;
for (auto &C : Path)
UpperDest.push_back(toUppercase(C));
if (!llvm::sys::fs::real_path(UpperDest, RealDest) && Path == RealDest)
return false;
return true;
}
void ModuleDependencyCollector::writeFileMap() {
if (Seen.empty())
return;
StringRef VFSDir = getDest();
VFSWriter.setOverlayDir(VFSDir);
VFSWriter.setCaseSensitivity(isCaseSensitivePath(VFSDir));
VFSWriter.setUseExternalNames(false);
std::error_code EC;
SmallString<256> YAMLPath = VFSDir;
llvm::sys::path::append(YAMLPath, "vfs.yaml");
llvm::raw_fd_ostream OS(YAMLPath, EC, llvm::sys::fs::OF_TextWithCRLF);
if (EC) {
HasErrors = true;
return;
}
VFSWriter.write(OS);
}
std::error_code ModuleDependencyCollector::copyToRoot(StringRef Src,
StringRef Dst) {
using namespace llvm::sys;
llvm::FileCollector::PathCanonicalizer::PathStorage Paths =
Canonicalizer.canonicalize(Src);
SmallString<256> CacheDst = getDest();
if (Dst.empty()) {
path::append(CacheDst, path::relative_path(Paths.CopyFrom));
} else {
if (!fs::exists(Dst))
return std::error_code();
path::append(CacheDst, Dst);
Paths.CopyFrom = Dst;
}
if (std::error_code EC = fs::create_directories(path::parent_path(CacheDst),
true))
return EC;
if (std::error_code EC = fs::copy_file(Paths.CopyFrom, CacheDst))
return EC;
addFileMapping(Paths.VirtualPath, CacheDst);
return std::error_code();
}
void ModuleDependencyCollector::addFile(StringRef Filename, StringRef FileDst) {
if (insertSeen(Filename))
if (copyToRoot(Filename, FileDst))
HasErrors = true;
}