15 #include "clang/Basic/SourceManager.h" 16 #include "clang/Driver/Options.h" 17 #include "clang/Frontend/CompilerInstance.h" 18 #include "clang/Frontend/FrontendActions.h" 20 #include "llvm/ADT/SmallString.h" 21 #include "llvm/Support/FileUtilities.h" 22 #include "llvm/Support/MemoryBuffer.h" 23 #include "llvm/Support/Path.h" 24 #include "llvm/Support/raw_ostream.h" 27 using namespace clang;
34 class ModuleMapTargetOptions :
public clang::TargetOptions {
36 ModuleMapTargetOptions() { Triple = llvm::sys::getDefaultTargetTriple(); }
43 ModularizeUtilities::ModularizeUtilities(std::vector<std::string> &InputPaths,
44 llvm::StringRef Prefix,
45 llvm::StringRef ProblemFilesListPath)
47 ProblemFilesPath(ProblemFilesListPath), HasModuleMap(false),
48 MissingHeaderCount(0),
50 LangOpts(new LangOptions()), DiagIDs(new DiagnosticIDs()),
51 DiagnosticOpts(new DiagnosticOptions()),
52 DC(
llvm::errs(), DiagnosticOpts.get()),
54 new DiagnosticsEngine(DiagIDs, DiagnosticOpts.get(), &DC, false)),
55 TargetOpts(new ModuleMapTargetOptions()),
56 Target(TargetInfo::CreateTargetInfo(*Diagnostics, TargetOpts)),
57 FileMgr(new FileManager(FileSystemOpts)),
58 SourceMgr(new SourceManager(*Diagnostics, *FileMgr, false)),
59 HeaderInfo(new HeaderSearch(std::make_shared<HeaderSearchOptions>(),
60 *SourceMgr, *Diagnostics, *LangOpts,
66 std::vector<std::string> &InputPaths, llvm::StringRef Prefix,
67 llvm::StringRef ProblemFilesListPath) {
76 llvm::StringRef InputPath = *I;
78 if (InputPath.endswith(
".modulemap")) {
86 errs() <<
"modularize: error: Unable to get header list '" << InputPath
87 <<
"': " << EC.message() <<
'\n';
96 errs() <<
"modularize: error: Unable to get problem header list '" <<
ProblemFilesPath 97 <<
"': " << EC.message() <<
'\n';
101 return std::error_code();
120 for (ModuleMapIndex = 0; ModuleMapIndex < ModuleMapCount; ++ModuleMapIndex) {
121 std::unique_ptr<clang::ModuleMap> &ModMap =
ModuleMaps[ModuleMapIndex];
125 std::error_code LocalEC = Checker->doChecks();
126 if (LocalEC.value() > 0)
134 llvm::StringRef InputPath) {
137 SmallString<256> HeaderDirectory(InputPath);
138 llvm::sys::path::remove_filename(HeaderDirectory);
139 SmallString<256> CurrentDirectory;
140 llvm::sys::fs::current_path(CurrentDirectory);
147 ErrorOr<std::unique_ptr<MemoryBuffer>> listBuffer =
148 MemoryBuffer::getFile(InputPath);
149 if (std::error_code EC = listBuffer.getError())
153 SmallVector<StringRef, 32>
Strings;
154 listBuffer.get()->getBuffer().split(Strings,
"\n", -1,
false);
157 for (SmallVectorImpl<StringRef>::iterator I = Strings.begin(),
160 StringRef
Line = I->trim();
162 if (Line.empty() || (Line[0] ==
'#'))
164 std::pair<StringRef, StringRef> TargetAndDependents = Line.split(
':');
165 SmallString<256> HeaderFileName;
167 if (llvm::sys::path::is_absolute(TargetAndDependents.first))
168 llvm::sys::path::native(TargetAndDependents.first, HeaderFileName);
170 if (HeaderDirectory.size() != 0)
171 HeaderFileName = HeaderDirectory;
173 HeaderFileName = CurrentDirectory;
174 llvm::sys::path::append(HeaderFileName, TargetAndDependents.first);
175 llvm::sys::path::native(HeaderFileName);
179 SmallVector<StringRef, 4> DependentsList;
180 TargetAndDependents.second.split(DependentsList,
" ", -1,
false);
181 int Count = DependentsList.size();
183 SmallString<256> Dependent;
184 if (llvm::sys::path::is_absolute(DependentsList[
Index]))
185 Dependent = DependentsList[
Index];
187 if (HeaderDirectory.size() != 0)
188 Dependent = HeaderDirectory;
190 Dependent = CurrentDirectory;
191 llvm::sys::path::append(Dependent, DependentsList[Index]);
193 llvm::sys::path::native(Dependent);
202 return std::error_code();
207 llvm::StringRef InputPath) {
210 SmallString<256> HeaderDirectory(InputPath);
211 llvm::sys::path::remove_filename(HeaderDirectory);
212 SmallString<256> CurrentDirectory;
213 llvm::sys::fs::current_path(CurrentDirectory);
220 ErrorOr<std::unique_ptr<MemoryBuffer>> listBuffer =
221 MemoryBuffer::getFile(InputPath);
222 if (std::error_code EC = listBuffer.getError())
226 SmallVector<StringRef, 32>
Strings;
227 listBuffer.get()->getBuffer().split(Strings,
"\n", -1,
false);
230 for (SmallVectorImpl<StringRef>::iterator I = Strings.begin(),
233 StringRef
Line = I->trim();
235 if (Line.empty() || (Line[0] ==
'#'))
237 SmallString<256> HeaderFileName;
239 if (llvm::sys::path::is_absolute(Line))
240 llvm::sys::path::native(Line, HeaderFileName);
242 if (HeaderDirectory.size() != 0)
243 HeaderFileName = HeaderDirectory;
245 HeaderFileName = CurrentDirectory;
246 llvm::sys::path::append(HeaderFileName, Line);
247 llvm::sys::path::native(HeaderFileName);
254 return std::error_code();
259 llvm::StringRef InputPath) {
261 auto ModuleMapEntryOrErr =
262 SourceMgr->getFileManager().getFile(InputPath);
265 if (!ModuleMapEntryOrErr) {
266 llvm::errs() <<
"error: File \"" << InputPath <<
"\" not found.\n";
267 return ModuleMapEntryOrErr.getError();
269 const FileEntry *ModuleMapEntry = *ModuleMapEntryOrErr;
276 const DirectoryEntry *Dir = ModuleMapEntry->getDir();
277 StringRef DirName(Dir->getName());
278 if (llvm::sys::path::filename(DirName) ==
"Modules") {
279 DirName = llvm::sys::path::parent_path(DirName);
280 if (DirName.endswith(
".framework")) {
281 if (
auto DirEntry =
FileMgr->getDirectory(DirName))
288 assert(Dir &&
"parent must exist");
291 std::unique_ptr<ModuleMap> ModMap;
296 if (ModMap->parseModuleMapFile(ModuleMapEntry,
false, Dir)) {
297 return std::error_code(1, std::generic_category());
307 return std::error_code(1, std::generic_category());
317 return std::error_code(1, std::generic_category());
319 return std::error_code();
326 for (ModuleMap::module_iterator I = ModMap->module_begin(),
327 E = ModMap->module_end();
349 for (
auto MI = Mod.submodule_begin(), MIEnd = Mod.submodule_end();
353 if (
const FileEntry *UmbrellaHeader = Mod.getUmbrellaHeader().Entry) {
360 else if (
const DirectoryEntry *UmbrellaDir = Mod.getUmbrellaDir().Entry) {
362 if (Mod.Headers->size() == 0) {
374 int NormalHeaderCount = Mod.Headers[clang::Module::HK_Normal].size();
379 const clang::Module::Header &Header(
380 Mod.Headers[clang::Module::HK_Normal][
Index]);
385 int MissingCountThisModule = Mod.MissingHeaders.size();
388 std::string MissingFile = Mod.MissingHeaders[
Index].FileName;
389 SourceLocation
Loc = Mod.MissingHeaders[
Index].FileNameLoc;
391 <<
": error : Header not found: " << MissingFile <<
"\n";
403 SmallString<256>
Directory(UmbrellaDirName);
406 for (llvm::sys::fs::directory_iterator I(Directory.str(), EC),
E; I !=
E;
410 std::string File(I->path());
411 llvm::ErrorOr<llvm::sys::fs::basic_file_status> Status = I->status();
414 llvm::sys::fs::file_type
Type = Status->type();
416 if (Type == llvm::sys::fs::file_type::directory_file) {
426 Dependents.push_back(HeaderPath);
434 SmallString<128> Buffer;
435 llvm::sys::path::const_iterator B = llvm::sys::path::begin(Path),
436 E = llvm::sys::path::end(Path);
438 if (B->compare(
".") == 0) {
440 else if (B->compare(
"..") == 0)
441 llvm::sys::path::remove_filename(Buffer);
443 llvm::sys::path::append(Buffer, *B);
446 if (Path.endswith(
"/") || Path.endswith(
"\\"))
447 Buffer.append(1, Path.back());
448 return Buffer.c_str();
459 if (Tmp2.startswith(
"./"))
460 Tmp = Tmp2.substr(2);
470 StringRef Extension = llvm::sys::path::extension(FileName);
471 if (Extension.size() == 0)
473 if (Extension.equals_lower(
".h"))
475 if (Extension.equals_lower(
".inc"))
487 sys::path::remove_filename(Directory);
488 if (Directory.size() == 0)
490 return Directory.str();
499 if (TestFilePath == FilePath)
502 ProblemFileNames.push_back(FilePath);
514 errs() <<
"\nThese are the files with possible errors:\n\n";
516 errs() << ProblemFile <<
"\n";
522 errs() <<
"\nThese are the files with no detected errors:\n\n";
526 if (ProblemFile == GoodFile) {
532 errs() << GoodFile <<
"\n";
539 "\nThese are the combined files, with problem files preceded by #:\n\n";
543 if (ProblemFile == File) {
548 errs() << (Good ?
"" :
"#") << File <<
"\n";
ModularizeUtilities(std::vector< std::string > &InputPaths, llvm::StringRef Prefix, llvm::StringRef ProblemFilesListPath)
Constructor.
SourceLocation Loc
'#' location in the include directive
void displayGoodFiles()
List files with no problems.
DependencyMap Dependencies
Map of top-level header file dependencies.
std::error_code doCoverageCheck(std::vector< std::string > &IncludePaths, llvm::ArrayRef< std::string > CommandLine)
Do coverage checks.
llvm::IntrusiveRefCntPtr< clang::DiagnosticsEngine > Diagnostics
Diagnostic engine.
Some operations such as code completion produce a set of candidates.
static std::string getCanonicalPath(llvm::StringRef FilePath)
Convert header path to canonical form.
llvm::SmallVector< std::string, 4 > DependentsVector
void displayCombinedFiles()
List files with problem files commented out.
std::error_code loadProblemHeaderList(llvm::StringRef InputPath)
Load problem header list.
static std::string getDirectoryFromPath(llvm::StringRef Path)
Get directory path component from file path.
bool HasModuleMap
True if we have module maps.
static cl::opt< std::string > HeaderPrefix("prefix", cl::init(""), cl::desc("Prepend header file paths with this prefix." " If not specified," " the files are considered to be relative to the header list file."))
std::error_code loadSingleHeaderListsAndDependencies(llvm::StringRef InputPath)
Load single header list and dependencies.
llvm::IntrusiveRefCntPtr< clang::TargetInfo > Target
Target information.
static ModularizeUtilities * createModularizeUtilities(std::vector< std::string > &InputPaths, llvm::StringRef Prefix, llvm::StringRef ProblemFilesListPath)
Create instance of ModularizeUtilities.
llvm::IntrusiveRefCntPtr< clang::FileManager > FileMgr
File system manager.
clang::TextDiagnosticPrinter DC
Diagnostic consumer.
std::unique_ptr< clang::HeaderSearch > HeaderInfo
Header search manager.
std::vector< HeaderHandle > Path
static std::string replace(llvm::StringRef Haystack, llvm::StringRef Needle, llvm::StringRef Repl)
ModularizeUtilities class definition.
std::vector< llvm::StringRef > CommandLine
std::shared_ptr< clang::LangOptions > LangOpts
Options controlling the language variant.
bool collectModuleMapHeaders(clang::ModuleMap *ModMap)
Collect module Map headers.
llvm::StringRef ProblemFilesPath
The path of problem files list file.
Definitions for CoverageChecker.
int MissingHeaderCount
Missing header count.
std::error_code loadModuleMap(llvm::StringRef InputPath)
Load single module map and extract header file list.
static std::unique_ptr< CoverageChecker > createCoverageChecker(llvm::StringRef ModuleMapPath, std::vector< std::string > &IncludePaths, llvm::ArrayRef< std::string > CommandLine, clang::ModuleMap *ModuleMap)
Create instance of CoverageChecker.
std::error_code loadAllHeaderListsAndDependencies()
Load header list and dependencies.
llvm::SmallVector< std::string, 32 > ProblemFileNames
List of header files with problems.
llvm::SmallVector< std::string, 32 > GoodFileNames
List of header files with no problems during the first pass, that is, no compile errors.
Modularize utilities class.
llvm::StringRef Directory
std::vector< llvm::StringRef > Strings
llvm::SmallVector< std::string, 32 > HeaderFileNames
List of top-level header files.
llvm::IntrusiveRefCntPtr< clang::SourceManager > SourceMgr
Source manager.
static cl::list< std::string > IncludePaths("I", cl::desc("Include path for coverage check."), cl::ZeroOrMore, cl::value_desc("path"))
llvm::StringRef HeaderPrefix
The header prefix.
bool collectModuleHeaders(const clang::Module &Mod)
Collect referenced headers from one module.
===– Representation.cpp - ClangDoc Representation --------—*- C++ -*-===//
std::vector< std::unique_ptr< clang::ModuleMap > > ModuleMaps
bool collectUmbrellaHeaders(llvm::StringRef UmbrellaDirName, DependentsVector &Dependents)
Collect headers from an umbrella directory.
void addUniqueProblemFile(std::string FilePath)
Add unique problem file.
void displayProblemFiles()
List problem files.
std::vector< std::string > InputFilePaths
The input file paths.
static std::string replaceDotDot(StringRef Path)
static bool isHeader(llvm::StringRef FileName)
Check for header file extension.
void addNoCompileErrorsFile(std::string FilePath)
Add file with no compile errors.
const SymbolIndex * Index