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/Path.h"
25 #include "llvm/Support/raw_ostream.h"
27 using namespace clang;
30 class DependencyFileCallback : public PPCallbacks {
31 std::vector<std::string> Files;
32 llvm::StringSet<> FilesSet;
33 const Preprocessor *PP;
34 std::vector<std::string> Targets;
36 bool IncludeSystemHeaders;
38 bool AddMissingHeaderDeps;
40 bool FileMatchesDepCriteria(const char *Filename,
41 SrcMgr::CharacteristicKind FileType);
42 void AddFilename(StringRef Filename);
43 void OutputDependencyFile();
46 DependencyFileCallback(const Preprocessor *_PP,
48 const DependencyOutputOptions &Opts)
49 : PP(_PP), Targets(Opts.Targets), OS(_OS),
50 IncludeSystemHeaders(Opts.IncludeSystemHeaders),
51 PhonyTarget(Opts.UsePhonyTargets),
52 AddMissingHeaderDeps(Opts.AddMissingHeaderDeps) {}
54 virtual void FileChanged(SourceLocation Loc, FileChangeReason Reason,
55 SrcMgr::CharacteristicKind FileType,
57 virtual void InclusionDirective(SourceLocation HashLoc,
58 const Token &IncludeTok,
61 const FileEntry *File,
62 SourceLocation EndLoc,
64 StringRef RelativePath);
66 virtual void EndOfMainFile() {
67 OutputDependencyFile();
74 void clang::AttachDependencyFileGen(Preprocessor &PP,
75 const DependencyOutputOptions &Opts) {
76 if (Opts.Targets.empty()) {
77 PP.getDiagnostics().Report(diag::err_fe_dependency_file_requires_MT);
82 raw_ostream *OS(new llvm::raw_fd_ostream(Opts.OutputFile.c_str(), Err));
84 PP.getDiagnostics().Report(diag::err_fe_error_opening)
85 << Opts.OutputFile << Err;
89 // Disable the "file not found" diagnostic if the -MG option was given.
90 if (Opts.AddMissingHeaderDeps)
91 PP.SetSuppressIncludeNotFoundError(true);
93 PP.addPPCallbacks(new DependencyFileCallback(&PP, OS, Opts));
96 /// FileMatchesDepCriteria - Determine whether the given Filename should be
97 /// considered as a dependency.
98 bool DependencyFileCallback::FileMatchesDepCriteria(const char *Filename,
99 SrcMgr::CharacteristicKind FileType) {
100 if (strcmp("<built-in>", Filename) == 0)
103 if (IncludeSystemHeaders)
106 return FileType == SrcMgr::C_User;
109 void DependencyFileCallback::FileChanged(SourceLocation Loc,
110 FileChangeReason Reason,
111 SrcMgr::CharacteristicKind FileType,
113 if (Reason != PPCallbacks::EnterFile)
116 // Dependency generation really does want to go all the way to the
117 // file entry for a source location to find out what is depended on.
118 // We do not want #line markers to affect dependency generation!
119 SourceManager &SM = PP->getSourceManager();
121 const FileEntry *FE =
122 SM.getFileEntryForID(SM.getFileID(SM.getExpansionLoc(Loc)));
125 StringRef Filename = FE->getName();
126 if (!FileMatchesDepCriteria(Filename.data(), FileType))
129 // Remove leading "./" (or ".//" or "././" etc.)
130 while (Filename.size() > 2 && Filename[0] == '.' &&
131 llvm::sys::path::is_separator(Filename[1])) {
132 Filename = Filename.substr(1);
133 while (llvm::sys::path::is_separator(Filename[0]))
134 Filename = Filename.substr(1);
137 AddFilename(Filename);
140 void DependencyFileCallback::InclusionDirective(SourceLocation HashLoc,
141 const Token &IncludeTok,
144 const FileEntry *File,
145 SourceLocation EndLoc,
146 StringRef SearchPath,
147 StringRef RelativePath) {
148 if (AddMissingHeaderDeps && !File)
149 AddFilename(FileName);
152 void DependencyFileCallback::AddFilename(StringRef Filename) {
153 if (FilesSet.insert(Filename))
154 Files.push_back(Filename);
157 /// PrintFilename - GCC escapes spaces, but apparently not ' or " or other
158 /// scary characters.
159 static void PrintFilename(raw_ostream &OS, StringRef Filename) {
160 for (unsigned i = 0, e = Filename.size(); i != e; ++i) {
161 if (Filename[i] == ' ')
167 void DependencyFileCallback::OutputDependencyFile() {
168 // Write out the dependency targets, trying to avoid overly long
169 // lines when possible. We try our best to emit exactly the same
170 // dependency file as GCC (4.2), assuming the included files are the
172 const unsigned MaxColumns = 75;
173 unsigned Columns = 0;
175 for (std::vector<std::string>::iterator
176 I = Targets.begin(), E = Targets.end(); I != E; ++I) {
177 unsigned N = I->length();
180 } else if (Columns + N + 2 > MaxColumns) {
187 // Targets already quoted as needed.
194 // Now add each dependency in the order it was seen, but avoiding
196 for (std::vector<std::string>::iterator I = Files.begin(),
197 E = Files.end(); I != E; ++I) {
198 // Start a new line if this would exceed the column limit. Make
199 // sure to leave space for a trailing " \" in case we need to
200 // break the line on the next iteration.
201 unsigned N = I->length();
202 if (Columns + (N + 1) + 2 > MaxColumns) {
207 PrintFilename(*OS, *I);
212 // Create phony targets if requested.
213 if (PhonyTarget && !Files.empty()) {
214 // Skip the first entry, this is always the input file itself.
215 for (std::vector<std::string>::iterator I = Files.begin() + 1,
216 E = Files.end(); I != E; ++I) {
218 PrintFilename(*OS, *I);