Delete the depfile if we could not open a header file. We cannot

generate any reasonable depfile if a header is missing.

llvm-svn: 145019
This commit is contained in:
Peter Collingbourne 2011-11-21 00:01:14 +00:00
parent 119cfaa595
commit 0e7e3fc130
2 changed files with 43 additions and 28 deletions

View File

@ -31,11 +31,12 @@ class DependencyFileCallback : public PPCallbacks {
std::vector<std::string> Files; std::vector<std::string> Files;
llvm::StringSet<> FilesSet; llvm::StringSet<> FilesSet;
const Preprocessor *PP; const Preprocessor *PP;
std::string OutputFile;
std::vector<std::string> Targets; std::vector<std::string> Targets;
raw_ostream *OS;
bool IncludeSystemHeaders; bool IncludeSystemHeaders;
bool PhonyTarget; bool PhonyTarget;
bool AddMissingHeaderDeps; bool AddMissingHeaderDeps;
bool SeenMissingHeader;
private: private:
bool FileMatchesDepCriteria(const char *Filename, bool FileMatchesDepCriteria(const char *Filename,
SrcMgr::CharacteristicKind FileType); SrcMgr::CharacteristicKind FileType);
@ -44,12 +45,12 @@ private:
public: public:
DependencyFileCallback(const Preprocessor *_PP, DependencyFileCallback(const Preprocessor *_PP,
raw_ostream *_OS,
const DependencyOutputOptions &Opts) const DependencyOutputOptions &Opts)
: PP(_PP), Targets(Opts.Targets), OS(_OS), : PP(_PP), OutputFile(Opts.OutputFile), Targets(Opts.Targets),
IncludeSystemHeaders(Opts.IncludeSystemHeaders), IncludeSystemHeaders(Opts.IncludeSystemHeaders),
PhonyTarget(Opts.UsePhonyTargets), PhonyTarget(Opts.UsePhonyTargets),
AddMissingHeaderDeps(Opts.AddMissingHeaderDeps) {} AddMissingHeaderDeps(Opts.AddMissingHeaderDeps),
SeenMissingHeader(false) {}
virtual void FileChanged(SourceLocation Loc, FileChangeReason Reason, virtual void FileChanged(SourceLocation Loc, FileChangeReason Reason,
SrcMgr::CharacteristicKind FileType, SrcMgr::CharacteristicKind FileType,
@ -65,8 +66,6 @@ public:
virtual void EndOfMainFile() { virtual void EndOfMainFile() {
OutputDependencyFile(); OutputDependencyFile();
delete OS;
OS = 0;
} }
}; };
} }
@ -78,19 +77,11 @@ void clang::AttachDependencyFileGen(Preprocessor &PP,
return; return;
} }
std::string Err;
raw_ostream *OS(new llvm::raw_fd_ostream(Opts.OutputFile.c_str(), Err));
if (!Err.empty()) {
PP.getDiagnostics().Report(diag::err_fe_error_opening)
<< Opts.OutputFile << Err;
return;
}
// Disable the "file not found" diagnostic if the -MG option was given. // Disable the "file not found" diagnostic if the -MG option was given.
if (Opts.AddMissingHeaderDeps) if (Opts.AddMissingHeaderDeps)
PP.SetSuppressIncludeNotFoundError(true); PP.SetSuppressIncludeNotFoundError(true);
PP.addPPCallbacks(new DependencyFileCallback(&PP, OS, Opts)); PP.addPPCallbacks(new DependencyFileCallback(&PP, Opts));
} }
/// FileMatchesDepCriteria - Determine whether the given Filename should be /// FileMatchesDepCriteria - Determine whether the given Filename should be
@ -145,8 +136,12 @@ void DependencyFileCallback::InclusionDirective(SourceLocation HashLoc,
SourceLocation EndLoc, SourceLocation EndLoc,
StringRef SearchPath, StringRef SearchPath,
StringRef RelativePath) { StringRef RelativePath) {
if (AddMissingHeaderDeps && !File) if (!File) {
AddFilename(FileName); if (AddMissingHeaderDeps)
AddFilename(FileName);
else
SeenMissingHeader = true;
}
} }
void DependencyFileCallback::AddFilename(StringRef Filename) { void DependencyFileCallback::AddFilename(StringRef Filename) {
@ -165,6 +160,19 @@ static void PrintFilename(raw_ostream &OS, StringRef Filename) {
} }
void DependencyFileCallback::OutputDependencyFile() { void DependencyFileCallback::OutputDependencyFile() {
if (SeenMissingHeader) {
llvm::sys::Path(OutputFile).eraseFromDisk();
return;
}
std::string Err;
llvm::raw_fd_ostream OS(OutputFile.c_str(), Err);
if (!Err.empty()) {
PP->getDiagnostics().Report(diag::err_fe_error_opening)
<< OutputFile << Err;
return;
}
// Write out the dependency targets, trying to avoid overly long // Write out the dependency targets, trying to avoid overly long
// lines when possible. We try our best to emit exactly the same // lines when possible. We try our best to emit exactly the same
// dependency file as GCC (4.2), assuming the included files are the // dependency file as GCC (4.2), assuming the included files are the
@ -179,16 +187,16 @@ void DependencyFileCallback::OutputDependencyFile() {
Columns += N; Columns += N;
} else if (Columns + N + 2 > MaxColumns) { } else if (Columns + N + 2 > MaxColumns) {
Columns = N + 2; Columns = N + 2;
*OS << " \\\n "; OS << " \\\n ";
} else { } else {
Columns += N + 1; Columns += N + 1;
*OS << ' '; OS << ' ';
} }
// Targets already quoted as needed. // Targets already quoted as needed.
*OS << *I; OS << *I;
} }
*OS << ':'; OS << ':';
Columns += 1; Columns += 1;
// Now add each dependency in the order it was seen, but avoiding // Now add each dependency in the order it was seen, but avoiding
@ -200,23 +208,23 @@ void DependencyFileCallback::OutputDependencyFile() {
// break the line on the next iteration. // break the line on the next iteration.
unsigned N = I->length(); unsigned N = I->length();
if (Columns + (N + 1) + 2 > MaxColumns) { if (Columns + (N + 1) + 2 > MaxColumns) {
*OS << " \\\n "; OS << " \\\n ";
Columns = 2; Columns = 2;
} }
*OS << ' '; OS << ' ';
PrintFilename(*OS, *I); PrintFilename(OS, *I);
Columns += N + 1; Columns += N + 1;
} }
*OS << '\n'; OS << '\n';
// Create phony targets if requested. // Create phony targets if requested.
if (PhonyTarget && !Files.empty()) { if (PhonyTarget && !Files.empty()) {
// Skip the first entry, this is always the input file itself. // Skip the first entry, this is always the input file itself.
for (std::vector<std::string>::iterator I = Files.begin() + 1, for (std::vector<std::string>::iterator I = Files.begin() + 1,
E = Files.end(); I != E; ++I) { E = Files.end(); I != E; ++I) {
*OS << '\n'; OS << '\n';
PrintFilename(*OS, *I); PrintFilename(OS, *I);
*OS << ":\n"; OS << ":\n";
} }
} }
} }

View File

@ -3,6 +3,11 @@
// RUN: test ! -f %t.o // RUN: test ! -f %t.o
// RUN: test ! -f %t.d // RUN: test ! -f %t.d
// RUN: touch %t.o
// RUN: not %clang -DMISSING -o %t.o -MMD -MF %t.d %s
// RUN: test ! -f %t.o
// RUN: test ! -f %t.d
// RUN: touch %t.o // RUN: touch %t.o
// RUN: not %clang -o %t.o -MMD -MF %t.d %s // RUN: not %clang -o %t.o -MMD -MF %t.d %s
// RUN: test ! -f %t.o // RUN: test ! -f %t.o
@ -15,6 +20,8 @@
#ifdef CRASH #ifdef CRASH
#pragma clang __debug crash #pragma clang __debug crash
#elif defined(MISSING)
#include "nonexistent.h"
#else #else
invalid C code invalid C code
#endif #endif