1 //===--- DependencyFile.cpp - Generate dependency file --------------------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This code generates dependency files.
12 //===----------------------------------------------------------------------===//
14 #include "clang/Frontend/Utils.h"
15 #include "clang/Basic/FileManager.h"
16 #include "clang/Basic/SourceManager.h"
17 #include "clang/Frontend/DependencyOutputOptions.h"
18 #include "clang/Frontend/FrontendDiagnostic.h"
19 #include "clang/Lex/DirectoryLookup.h"
20 #include "clang/Lex/LexDiagnostic.h"
21 #include "clang/Lex/PPCallbacks.h"
22 #include "clang/Lex/Preprocessor.h"
23 #include "llvm/ADT/StringSet.h"
24 #include "llvm/Support/FileSystem.h"
25 #include "llvm/Support/Path.h"
26 #include "llvm/Support/raw_ostream.h"
28 using namespace clang;
31 class DependencyFileCallback : public PPCallbacks {
32 std::vector<std::string> Files;
33 llvm::StringSet<> FilesSet;
34 const Preprocessor *PP;
35 std::string OutputFile;
36 std::vector<std::string> Targets;
37 bool IncludeSystemHeaders;
39 bool AddMissingHeaderDeps;
40 bool SeenMissingHeader;
42 bool FileMatchesDepCriteria(const char *Filename,
43 SrcMgr::CharacteristicKind FileType);
44 void AddFilename(StringRef Filename);
45 void OutputDependencyFile();
48 DependencyFileCallback(const Preprocessor *_PP,
49 const DependencyOutputOptions &Opts)
50 : PP(_PP), OutputFile(Opts.OutputFile), Targets(Opts.Targets),
51 IncludeSystemHeaders(Opts.IncludeSystemHeaders),
52 PhonyTarget(Opts.UsePhonyTargets),
53 AddMissingHeaderDeps(Opts.AddMissingHeaderDeps),
54 SeenMissingHeader(false) {}
56 virtual void FileChanged(SourceLocation Loc, FileChangeReason Reason,
57 SrcMgr::CharacteristicKind FileType,
59 virtual void InclusionDirective(SourceLocation HashLoc,
60 const Token &IncludeTok,
63 CharSourceRange FilenameRange,
64 const FileEntry *File,
66 StringRef RelativePath,
67 const Module *Imported);
69 virtual void EndOfMainFile() {
70 OutputDependencyFile();
75 void clang::AttachDependencyFileGen(Preprocessor &PP,
76 const DependencyOutputOptions &Opts) {
77 if (Opts.Targets.empty()) {
78 PP.getDiagnostics().Report(diag::err_fe_dependency_file_requires_MT);
82 // Disable the "file not found" diagnostic if the -MG option was given.
83 if (Opts.AddMissingHeaderDeps)
84 PP.SetSuppressIncludeNotFoundError(true);
86 PP.addPPCallbacks(new DependencyFileCallback(&PP, Opts));
89 /// FileMatchesDepCriteria - Determine whether the given Filename should be
90 /// considered as a dependency.
91 bool DependencyFileCallback::FileMatchesDepCriteria(const char *Filename,
92 SrcMgr::CharacteristicKind FileType) {
93 if (strcmp("<built-in>", Filename) == 0)
96 if (IncludeSystemHeaders)
99 return FileType == SrcMgr::C_User;
102 void DependencyFileCallback::FileChanged(SourceLocation Loc,
103 FileChangeReason Reason,
104 SrcMgr::CharacteristicKind FileType,
106 if (Reason != PPCallbacks::EnterFile)
109 // Dependency generation really does want to go all the way to the
110 // file entry for a source location to find out what is depended on.
111 // We do not want #line markers to affect dependency generation!
112 SourceManager &SM = PP->getSourceManager();
114 const FileEntry *FE =
115 SM.getFileEntryForID(SM.getFileID(SM.getExpansionLoc(Loc)));
118 StringRef Filename = FE->getName();
119 if (!FileMatchesDepCriteria(Filename.data(), FileType))
122 // Remove leading "./" (or ".//" or "././" etc.)
123 while (Filename.size() > 2 && Filename[0] == '.' &&
124 llvm::sys::path::is_separator(Filename[1])) {
125 Filename = Filename.substr(1);
126 while (llvm::sys::path::is_separator(Filename[0]))
127 Filename = Filename.substr(1);
130 AddFilename(Filename);
133 void DependencyFileCallback::InclusionDirective(SourceLocation HashLoc,
134 const Token &IncludeTok,
137 CharSourceRange FilenameRange,
138 const FileEntry *File,
139 StringRef SearchPath,
140 StringRef RelativePath,
141 const Module *Imported) {
143 if (AddMissingHeaderDeps)
144 AddFilename(FileName);
146 SeenMissingHeader = true;
150 void DependencyFileCallback::AddFilename(StringRef Filename) {
151 if (FilesSet.insert(Filename))
152 Files.push_back(Filename);
155 /// PrintFilename - GCC escapes spaces, # and $, but apparently not ' or " or
156 /// other scary characters.
157 static void PrintFilename(raw_ostream &OS, StringRef Filename) {
158 for (unsigned i = 0, e = Filename.size(); i != e; ++i) {
159 if (Filename[i] == ' ' || Filename[i] == '#')
161 else if (Filename[i] == '$') // $ is escaped by $$.
167 void DependencyFileCallback::OutputDependencyFile() {
168 if (SeenMissingHeader) {
170 llvm::sys::fs::remove(OutputFile, existed);
175 llvm::raw_fd_ostream OS(OutputFile.c_str(), Err);
177 PP->getDiagnostics().Report(diag::err_fe_error_opening)
178 << OutputFile << Err;
182 // Write out the dependency targets, trying to avoid overly long
183 // lines when possible. We try our best to emit exactly the same
184 // dependency file as GCC (4.2), assuming the included files are the
186 const unsigned MaxColumns = 75;
187 unsigned Columns = 0;
189 for (std::vector<std::string>::iterator
190 I = Targets.begin(), E = Targets.end(); I != E; ++I) {
191 unsigned N = I->length();
194 } else if (Columns + N + 2 > MaxColumns) {
201 // Targets already quoted as needed.
208 // Now add each dependency in the order it was seen, but avoiding
210 for (std::vector<std::string>::iterator I = Files.begin(),
211 E = Files.end(); I != E; ++I) {
212 // Start a new line if this would exceed the column limit. Make
213 // sure to leave space for a trailing " \" in case we need to
214 // break the line on the next iteration.
215 unsigned N = I->length();
216 if (Columns + (N + 1) + 2 > MaxColumns) {
221 PrintFilename(OS, *I);
226 // Create phony targets if requested.
227 if (PhonyTarget && !Files.empty()) {
228 // Skip the first entry, this is always the input file itself.
229 for (std::vector<std::string>::iterator I = Files.begin() + 1,
230 E = Files.end(); I != E; ++I) {
232 PrintFilename(OS, *I);