Skip to content

Instantly share code, notes, and snippets.

@pieter
Created March 20, 2009 17:57
Show Gist options
  • Save pieter/82471 to your computer and use it in GitHub Desktop.
Save pieter/82471 to your computer and use it in GitHub Desktop.
diff --git a/Driver/DependencyFile.cpp b/Driver/DependencyFile.cpp
index 673ca07..8ffbfdf 100644
--- a/Driver/DependencyFile.cpp
+++ b/Driver/DependencyFile.cpp
@@ -25,6 +25,7 @@
#include "llvm/Support/raw_ostream.h"
#include <fstream>
#include <string>
+#include <iostream>
using namespace clang;
@@ -33,9 +34,9 @@ class VISIBILITY_HIDDEN DependencyFileCallback : public PPCallbacks {
std::vector<std::string> Files;
llvm::StringSet<> FilesSet;
const Preprocessor *PP;
- std::ofstream OS;
const std::string &InputFile;
std::vector<std::string> Targets;
+ std::ostream *OS;
private:
bool FileMatchesDepCriteria(const char *Filename,
@@ -48,6 +49,10 @@ public:
const std::string &DepFile,
const std::vector<std::string> &Targets,
const char *&ErrStr);
+ DependencyFileCallback(const Preprocessor *PP,
+ const std::string &InputFile,
+ const std::vector<std::string> &Targets);
+
~DependencyFileCallback();
virtual void FileChanged(SourceLocation Loc, FileChangeReason Reason,
SrcMgr::CharacteristicKind FileType);
@@ -84,15 +89,20 @@ PhonyDependencyTarget("MP",
llvm::cl::desc("Create phony target for each dependency "
"(other than main file)"));
+// FIXME: Implement feature
+static llvm::cl::opt<bool>
+GenerateDependencyOut("M",
+ llvm::cl::desc("Output dependency to STDIN"));
+
+
bool clang::CreateDependencyFileGen(Preprocessor *PP,
std::string &OutputFile,
const std::string &InputFile,
const char *&ErrStr) {
assert(!InputFile.empty() && "No file given");
-
ErrStr = NULL;
- if (!GenerateDependencyFile && !GenerateDependencyFileNoSysHeaders) {
+ if (!(GenerateDependencyFile || GenerateDependencyFileNoSysHeaders || GenerateDependencyOut)) {
if (!DependencyOutputFile.empty() || !DependencyTargets.empty() ||
PhonyDependencyTarget)
ErrStr = "Error: to generate dependencies you must specify -MD or -MMD\n";
@@ -135,8 +145,13 @@ bool clang::CreateDependencyFileGen(Preprocessor *PP,
}
}
- DependencyFileCallback *PPDep =
- new DependencyFileCallback(PP, InputFile, DepFile.toString(),
+ DependencyFileCallback *PPDep;
+ if (GenerateDependencyOut)
+ {
+ PPDep = new DependencyFileCallback(PP, InputFile, Targets);
+ }
+ else
+ PPDep = new DependencyFileCallback(PP, InputFile, DepFile.toString(),
Targets, ErrStr);
if (ErrStr){
delete PPDep;
@@ -152,6 +167,7 @@ bool clang::CreateDependencyFileGen(Preprocessor *PP,
/// considered as a dependency.
bool DependencyFileCallback::FileMatchesDepCriteria(const char *Filename,
SrcMgr::CharacteristicKind FileType) {
+
if (strcmp(InputFile.c_str(), Filename) != 0 &&
strcmp("<predefines>", Filename) != 0) {
if (GenerateDependencyFileNoSysHeaders)
@@ -166,6 +182,7 @@ bool DependencyFileCallback::FileMatchesDepCriteria(const char *Filename,
void DependencyFileCallback::FileChanged(SourceLocation Loc,
FileChangeReason Reason,
SrcMgr::CharacteristicKind FileType) {
+
if (Reason != PPCallbacks::EnterFile)
return;
@@ -198,22 +215,23 @@ void DependencyFileCallback::OutputDependencyFile() {
const unsigned MaxColumns = 75;
unsigned Columns = 0;
+ std::ostream &OFS = *OS;
for (std::vector<std::string>::iterator
I = Targets.begin(), E = Targets.end(); I != E; ++I) {
unsigned N = I->length();
if (Columns == 0) {
Columns += N;
- OS << *I;
+ OFS << *I;
} else if (Columns + N + 2 > MaxColumns) {
Columns = N + 2;
- OS << " \\\n " << *I;
+ OFS << " \\\n " << *I;
} else {
Columns += N + 1;
- OS << " " << *I;
+ OFS << " " << *I;
}
}
- OS << ":";
+ OFS << ":";
Columns += 1;
// Now add each dependency in the order it was seen, but avoiding
@@ -225,25 +243,33 @@ void DependencyFileCallback::OutputDependencyFile() {
// break the line on the next iteration.
unsigned N = I->length();
if (Columns + (N + 1) + 2 > MaxColumns) {
- OS << " \\\n ";
+ OFS << " \\\n ";
Columns = 2;
}
- OS << " " << *I;
+ OFS << " " << *I;
Columns += N + 1;
}
- OS << "\n";
+ OFS << "\n";
// Create phony targets if requested.
if (PhonyDependencyTarget) {
// Skip the first entry, this is always the input file itself.
for (std::vector<std::string>::iterator I = Files.begin() + 1,
E = Files.end(); I != E; ++I) {
- OS << "\n";
- OS << *I << ":\n";
+ OFS << "\n";
+ OFS << *I << ":\n";
}
}
}
+DependencyFileCallback::DependencyFileCallback(const Preprocessor *PP,
+ const std::string &InputFile,
+ const std::vector<std::string> &Targets)
+ : PP(PP), InputFile(InputFile), Targets(Targets) {
+ OS = &std::cout;
+ Files.push_back(InputFile);
+ }
+
DependencyFileCallback::DependencyFileCallback(const Preprocessor *PP,
const std::string &InputFile,
const std::string &DepFile,
@@ -251,9 +277,12 @@ DependencyFileCallback::DependencyFileCallback(const Preprocessor *PP,
&Targets,
const char *&ErrStr)
: PP(PP), InputFile(InputFile), Targets(Targets) {
+
+ std::ofstream *OFS = new std::ofstream;
+ OFS->open(DepFile.c_str());
+ OS = OFS;
- OS.open(DepFile.c_str());
- if (OS.fail())
+ if (OS->fail())
ErrStr = "Could not open dependency output file\n";
else
ErrStr = NULL;
@@ -262,11 +291,16 @@ DependencyFileCallback::DependencyFileCallback(const Preprocessor *PP,
}
DependencyFileCallback::~DependencyFileCallback() {
- if ((!GenerateDependencyFile && !GenerateDependencyFileNoSysHeaders) ||
- OS.fail())
+
+ if ((!GenerateDependencyOut && !GenerateDependencyFile && !GenerateDependencyFileNoSysHeaders) ||
+ OS->fail())
return;
OutputDependencyFile();
- OS.close();
+ if (OS != &std::cout) {
+ std::ofstream *OFS = (std::ofstream *)OS;
+ OFS->close();
+ delete OS;
+ }
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment