diff options
-rw-r--r-- | test/Preprocessor/depencies-and-pp.c | 4 | ||||
-rw-r--r-- | tools/clang-cc/DependencyFile.cpp | 47 | ||||
-rw-r--r-- | tools/clang-cc/clang-cc.cpp | 13 |
3 files changed, 53 insertions, 11 deletions
diff --git a/test/Preprocessor/depencies-and-pp.c b/test/Preprocessor/depencies-and-pp.c new file mode 100644 index 0000000000..ec66c73f75 --- /dev/null +++ b/test/Preprocessor/depencies-and-pp.c @@ -0,0 +1,4 @@ +// RUN: clang -E -o %t.1 %s && +// RUN: clang -E -MD -MF %t.d -MT foo -o %t.2 %s && +// RUN: diff %t.1 %t.2 && +// RUN: grep "foo:" %t.d diff --git a/tools/clang-cc/DependencyFile.cpp b/tools/clang-cc/DependencyFile.cpp index 34b6ecfcab..9ef209eeeb 100644 --- a/tools/clang-cc/DependencyFile.cpp +++ b/tools/clang-cc/DependencyFile.cpp @@ -35,6 +35,10 @@ class VISIBILITY_HIDDEN DependencyFileCallback : public PPCallbacks { std::vector<std::string> Targets; llvm::raw_ostream *OS; + // FIXME: This functionality should be moved into a common class for + // chaining callbacks. + PPCallbacks *PrevCallbacks; + private: bool FileMatchesDepCriteria(const char *Filename, SrcMgr::CharacteristicKind FileType); @@ -43,11 +47,14 @@ private: public: DependencyFileCallback(const Preprocessor *_PP, llvm::raw_ostream *_OS, - const std::vector<std::string> &_Targets) - : PP(_PP), Targets(_Targets), OS(_OS) { + const std::vector<std::string> &_Targets, + PPCallbacks *_PrevCallbacks) + : PP(_PP), Targets(_Targets), OS(_OS), PrevCallbacks(_PrevCallbacks) { } ~DependencyFileCallback() { + if (PrevCallbacks) + delete PrevCallbacks; OutputDependencyFile(); OS->flush(); delete OS; @@ -55,6 +62,32 @@ public: virtual void FileChanged(SourceLocation Loc, FileChangeReason Reason, SrcMgr::CharacteristicKind FileType); + + virtual void Ident(SourceLocation Loc, const std::string &str) { + if (PrevCallbacks) + PrevCallbacks->Ident(Loc, str); + } + + virtual void PragmaComment(SourceLocation Loc, const IdentifierInfo *Kind, + const std::string &Str) { + if (PrevCallbacks) + PrevCallbacks->PragmaComment(Loc, Kind, Str); + } + + virtual void MacroExpands(const Token &Id, const MacroInfo* MI) { + if (PrevCallbacks) + PrevCallbacks->MacroExpands(Id, MI); + } + + virtual void MacroDefined(const IdentifierInfo *II, const MacroInfo *MI) { + if (PrevCallbacks) + PrevCallbacks->MacroDefined(II, MI); + } + + virtual void MacroUndefined(const IdentifierInfo *II, const MacroInfo *MI) { + if (PrevCallbacks) + PrevCallbacks->MacroUndefined(II, MI); + } }; } @@ -102,8 +135,13 @@ bool clang::CreateDependencyFileGen(Preprocessor *PP, } } + // Claim any previous callbacks. + PPCallbacks *Prev = PP->getPPCallbacks(); + if (Prev) + PP->setPPCallbacks(0); + DependencyFileCallback *PPDep = - new DependencyFileCallback(PP, OS, DependencyTargets); + new DependencyFileCallback(PP, OS, DependencyTargets, Prev); PP->setPPCallbacks(PPDep); return true; } @@ -124,6 +162,9 @@ bool DependencyFileCallback::FileMatchesDepCriteria(const char *Filename, void DependencyFileCallback::FileChanged(SourceLocation Loc, FileChangeReason Reason, SrcMgr::CharacteristicKind FileType) { + if (PrevCallbacks) + PrevCallbacks->FileChanged(Loc, Reason, FileType); + if (Reason != PPCallbacks::EnterFile) return; diff --git a/tools/clang-cc/clang-cc.cpp b/tools/clang-cc/clang-cc.cpp index 2ef1087e5c..a8836887a7 100644 --- a/tools/clang-cc/clang-cc.cpp +++ b/tools/clang-cc/clang-cc.cpp @@ -1385,14 +1385,11 @@ public: if (InitializePreprocessor(*PP, InFile, InitOpts)) return 0; - /// FIXME: PP can only handle one callback - if (ProgAction != PrintPreprocessedInput) { - std::string ErrStr; - bool DFG = CreateDependencyFileGen(PP.get(), ErrStr); - if (!DFG && !ErrStr.empty()) { - fprintf(stderr, "%s", ErrStr.c_str()); - return 0; - } + std::string ErrStr; + bool DFG = CreateDependencyFileGen(PP.get(), ErrStr); + if (!DFG && !ErrStr.empty()) { + fprintf(stderr, "%s", ErrStr.c_str()); + return 0; } return PP.take(); |