#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;
public:
ModuleDependencyListener(ModuleDependencyCollector &Collector)
: Collector(Collector) {}
bool needsInputFileVisitation() override { return true; }
bool needsSystemInputFileVisitation() override { return true; }
bool visitInputFile(StringRef Filename, bool IsSystem, bool IsOverridden,
bool IsExplicitModule) override {
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,
Optional<FileEntryRef> File, StringRef SearchPath,
StringRef RelativePath, const Module *Imported,
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(FileManager *FileMgr,
const FileEntry *Header) override {
StringRef HeaderFilename = Header->getName();
moduleMapAddHeader(HeaderFilename);
StringRef UmbreallDirFromHeader =
llvm::sys::path::parent_path(HeaderFilename);
StringRef UmbrellaDir = Header->getDir()->getName();
if (!UmbrellaDir.equals(UmbreallDirFromHeader)) {
SmallString<128> AltHeaderFilename;
llvm::sys::path::append(AltHeaderFilename, UmbrellaDir,
llvm::sys::path::filename(HeaderFilename));
if (FileMgr->getFile(AltHeaderFilename))
moduleMapAddHeader(AltHeaderFilename);
}
}
};
}
void ModuleDependencyCollector::attachToASTReader(ASTReader &R) {
R.addListener(std::make_unique<ModuleDependencyListener>(*this));
}
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.equals(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;
}