DependencyFile.cpp 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223
  1. //===--- DependencyFile.cpp - Generate dependency file --------------------===//
  2. //
  3. // The LLVM Compiler Infrastructure
  4. //
  5. // This file is distributed under the University of Illinois Open Source
  6. // License. See LICENSE.TXT for details.
  7. //
  8. //===----------------------------------------------------------------------===//
  9. //
  10. // This code generates dependency files.
  11. //
  12. //===----------------------------------------------------------------------===//
  13. #include "clang/Frontend/Utils.h"
  14. #include "clang/Basic/FileManager.h"
  15. #include "clang/Basic/SourceManager.h"
  16. #include "clang/Frontend/DependencyOutputOptions.h"
  17. #include "clang/Frontend/FrontendDiagnostic.h"
  18. #include "clang/Lex/DirectoryLookup.h"
  19. #include "clang/Lex/LexDiagnostic.h"
  20. #include "clang/Lex/PPCallbacks.h"
  21. #include "clang/Lex/Preprocessor.h"
  22. #include "llvm/ADT/StringSet.h"
  23. #include "llvm/Support/Path.h"
  24. #include "llvm/Support/raw_ostream.h"
  25. using namespace clang;
  26. namespace {
  27. class DependencyFileCallback : public PPCallbacks {
  28. std::vector<std::string> Files;
  29. llvm::StringSet<> FilesSet;
  30. const Preprocessor *PP;
  31. std::vector<std::string> Targets;
  32. raw_ostream *OS;
  33. bool IncludeSystemHeaders;
  34. bool PhonyTarget;
  35. bool AddMissingHeaderDeps;
  36. private:
  37. bool FileMatchesDepCriteria(const char *Filename,
  38. SrcMgr::CharacteristicKind FileType);
  39. void AddFilename(StringRef Filename);
  40. void OutputDependencyFile();
  41. public:
  42. DependencyFileCallback(const Preprocessor *_PP,
  43. raw_ostream *_OS,
  44. const DependencyOutputOptions &Opts)
  45. : PP(_PP), Targets(Opts.Targets), OS(_OS),
  46. IncludeSystemHeaders(Opts.IncludeSystemHeaders),
  47. PhonyTarget(Opts.UsePhonyTargets),
  48. AddMissingHeaderDeps(Opts.AddMissingHeaderDeps) {}
  49. virtual void FileChanged(SourceLocation Loc, FileChangeReason Reason,
  50. SrcMgr::CharacteristicKind FileType,
  51. FileID PrevFID);
  52. virtual void InclusionDirective(SourceLocation HashLoc,
  53. const Token &IncludeTok,
  54. StringRef FileName,
  55. bool IsAngled,
  56. const FileEntry *File,
  57. SourceLocation EndLoc,
  58. StringRef SearchPath,
  59. StringRef RelativePath);
  60. virtual void EndOfMainFile() {
  61. OutputDependencyFile();
  62. delete OS;
  63. OS = 0;
  64. }
  65. };
  66. }
  67. void clang::AttachDependencyFileGen(Preprocessor &PP,
  68. const DependencyOutputOptions &Opts) {
  69. if (Opts.Targets.empty()) {
  70. PP.getDiagnostics().Report(diag::err_fe_dependency_file_requires_MT);
  71. return;
  72. }
  73. std::string Err;
  74. raw_ostream *OS(new llvm::raw_fd_ostream(Opts.OutputFile.c_str(), Err));
  75. if (!Err.empty()) {
  76. PP.getDiagnostics().Report(diag::err_fe_error_opening)
  77. << Opts.OutputFile << Err;
  78. return;
  79. }
  80. // Disable the "file not found" diagnostic if the -MG option was given.
  81. if (Opts.AddMissingHeaderDeps)
  82. PP.SetSuppressIncludeNotFoundError(true);
  83. PP.addPPCallbacks(new DependencyFileCallback(&PP, OS, Opts));
  84. }
  85. /// FileMatchesDepCriteria - Determine whether the given Filename should be
  86. /// considered as a dependency.
  87. bool DependencyFileCallback::FileMatchesDepCriteria(const char *Filename,
  88. SrcMgr::CharacteristicKind FileType) {
  89. if (strcmp("<built-in>", Filename) == 0)
  90. return false;
  91. if (IncludeSystemHeaders)
  92. return true;
  93. return FileType == SrcMgr::C_User;
  94. }
  95. void DependencyFileCallback::FileChanged(SourceLocation Loc,
  96. FileChangeReason Reason,
  97. SrcMgr::CharacteristicKind FileType,
  98. FileID PrevFID) {
  99. if (Reason != PPCallbacks::EnterFile)
  100. return;
  101. // Dependency generation really does want to go all the way to the
  102. // file entry for a source location to find out what is depended on.
  103. // We do not want #line markers to affect dependency generation!
  104. SourceManager &SM = PP->getSourceManager();
  105. const FileEntry *FE =
  106. SM.getFileEntryForID(SM.getFileID(SM.getExpansionLoc(Loc)));
  107. if (FE == 0) return;
  108. StringRef Filename = FE->getName();
  109. if (!FileMatchesDepCriteria(Filename.data(), FileType))
  110. return;
  111. // Remove leading "./" (or ".//" or "././" etc.)
  112. while (Filename.size() > 2 && Filename[0] == '.' &&
  113. llvm::sys::path::is_separator(Filename[1])) {
  114. Filename = Filename.substr(1);
  115. while (llvm::sys::path::is_separator(Filename[0]))
  116. Filename = Filename.substr(1);
  117. }
  118. AddFilename(Filename);
  119. }
  120. void DependencyFileCallback::InclusionDirective(SourceLocation HashLoc,
  121. const Token &IncludeTok,
  122. StringRef FileName,
  123. bool IsAngled,
  124. const FileEntry *File,
  125. SourceLocation EndLoc,
  126. StringRef SearchPath,
  127. StringRef RelativePath) {
  128. if (AddMissingHeaderDeps && !File)
  129. AddFilename(FileName);
  130. }
  131. void DependencyFileCallback::AddFilename(StringRef Filename) {
  132. if (FilesSet.insert(Filename))
  133. Files.push_back(Filename);
  134. }
  135. /// PrintFilename - GCC escapes spaces, but apparently not ' or " or other
  136. /// scary characters.
  137. static void PrintFilename(raw_ostream &OS, StringRef Filename) {
  138. for (unsigned i = 0, e = Filename.size(); i != e; ++i) {
  139. if (Filename[i] == ' ')
  140. OS << '\\';
  141. OS << Filename[i];
  142. }
  143. }
  144. void DependencyFileCallback::OutputDependencyFile() {
  145. // Write out the dependency targets, trying to avoid overly long
  146. // lines when possible. We try our best to emit exactly the same
  147. // dependency file as GCC (4.2), assuming the included files are the
  148. // same.
  149. const unsigned MaxColumns = 75;
  150. unsigned Columns = 0;
  151. for (std::vector<std::string>::iterator
  152. I = Targets.begin(), E = Targets.end(); I != E; ++I) {
  153. unsigned N = I->length();
  154. if (Columns == 0) {
  155. Columns += N;
  156. } else if (Columns + N + 2 > MaxColumns) {
  157. Columns = N + 2;
  158. *OS << " \\\n ";
  159. } else {
  160. Columns += N + 1;
  161. *OS << ' ';
  162. }
  163. // Targets already quoted as needed.
  164. *OS << *I;
  165. }
  166. *OS << ':';
  167. Columns += 1;
  168. // Now add each dependency in the order it was seen, but avoiding
  169. // duplicates.
  170. for (std::vector<std::string>::iterator I = Files.begin(),
  171. E = Files.end(); I != E; ++I) {
  172. // Start a new line if this would exceed the column limit. Make
  173. // sure to leave space for a trailing " \" in case we need to
  174. // break the line on the next iteration.
  175. unsigned N = I->length();
  176. if (Columns + (N + 1) + 2 > MaxColumns) {
  177. *OS << " \\\n ";
  178. Columns = 2;
  179. }
  180. *OS << ' ';
  181. PrintFilename(*OS, *I);
  182. Columns += N + 1;
  183. }
  184. *OS << '\n';
  185. // Create phony targets if requested.
  186. if (PhonyTarget && !Files.empty()) {
  187. // Skip the first entry, this is always the input file itself.
  188. for (std::vector<std::string>::iterator I = Files.begin() + 1,
  189. E = Files.end(); I != E; ++I) {
  190. *OS << '\n';
  191. PrintFilename(*OS, *I);
  192. *OS << ":\n";
  193. }
  194. }
  195. }