ModuleDependencyCollector.cpp [plain text]
#include "clang/Basic/CharInfo.h"
#include "clang/Frontend/Utils.h"
#include "clang/Lex/Preprocessor.h"
#include "clang/Serialization/ASTReader.h"
#include "llvm/ADT/StringMap.h"
#include "llvm/ADT/iterator_range.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 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);
}
}
};
}
static bool real_path(StringRef SrcPath, SmallVectorImpl<char> &RealPath) {
#ifdef LLVM_ON_UNIX
char CanonicalPath[PATH_MAX];
if (!realpath(SrcPath.str().c_str(), CanonicalPath))
return false;
SmallString<256> RPath(CanonicalPath);
RealPath.swap(RPath);
return true;
#else
return false;
#endif
}
void ModuleDependencyCollector::attachToASTReader(ASTReader &R) {
R.addListener(llvm::make_unique<ModuleDependencyListener>(*this));
}
void ModuleDependencyCollector::attachToPreprocessor(Preprocessor &PP) {
PP.getHeaderSearchInfo().getModuleMap().addModuleMapCallbacks(
llvm::make_unique<ModuleDependencyMMCallbacks>(*this));
}
static bool isCaseSensitivePath(StringRef Path) {
SmallString<256> TmpDest = Path, UpperDest, RealDest;
if (!real_path(Path, TmpDest))
return true; Path = TmpDest;
for (auto &C : Path)
UpperDest.push_back(toUppercase(C));
if (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::F_Text);
if (EC) {
HasErrors = true;
return;
}
VFSWriter.write(OS);
}
bool ModuleDependencyCollector::getRealPath(StringRef SrcPath,
SmallVectorImpl<char> &Result) {
using namespace llvm::sys;
SmallString<256> RealPath;
StringRef FileName = path::filename(SrcPath);
std::string Dir = path::parent_path(SrcPath).str();
auto DirWithSymLink = SymLinkMap.find(Dir);
if (DirWithSymLink == SymLinkMap.end()) {
if (!real_path(Dir, RealPath))
return false;
SymLinkMap[Dir] = RealPath.str();
} else {
RealPath = DirWithSymLink->second;
}
path::append(RealPath, FileName);
Result.swap(RealPath);
return true;
}
std::error_code ModuleDependencyCollector::copyToRoot(StringRef Src) {
using namespace llvm::sys;
SmallString<256> AbsoluteSrc = Src;
fs::make_absolute(AbsoluteSrc);
path::native(AbsoluteSrc);
AbsoluteSrc = path::remove_leading_dotslash(AbsoluteSrc);
SmallString<256> CanonicalPath = AbsoluteSrc;
path::remove_dots(CanonicalPath, true);
SmallString<256> RealPath;
if (!getRealPath(AbsoluteSrc, RealPath))
RealPath = CanonicalPath;
SmallString<256> Dest = getDest();
path::append(Dest, path::relative_path(RealPath));
if (std::error_code EC = fs::create_directories(path::parent_path(Dest),
true))
return EC;
if (std::error_code EC = fs::copy_file(RealPath, Dest))
return EC;
addFileMapping(CanonicalPath, Dest);
return std::error_code();
}
void ModuleDependencyCollector::addFile(StringRef Filename) {
if (insertSeen(Filename))
if (copyToRoot(Filename))
HasErrors = true;
}