blob: 51dec967cd6bd8fff530e9ede37248c0e459dda7 [file] [log] [blame]
Daniel Dunbareef63e02011-02-02 15:41:17 +00001//===--- HeaderIncludes.cpp - Generate Header Includes --------------------===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9
10#include "clang/Frontend/Utils.h"
11#include "clang/Basic/SourceManager.h"
Daniel Dunbarf704c612011-02-03 03:45:00 +000012#include "clang/Frontend/FrontendDiagnostic.h"
Daniel Dunbareef63e02011-02-02 15:41:17 +000013#include "clang/Lex/Preprocessor.h"
Daniel Dunbarf704c612011-02-03 03:45:00 +000014#include "llvm/Support/raw_ostream.h"
Daniel Dunbareef63e02011-02-02 15:41:17 +000015using namespace clang;
16
17namespace {
18class HeaderIncludesCallback : public PPCallbacks {
19 SourceManager &SM;
Daniel Dunbarf704c612011-02-03 03:45:00 +000020 llvm::raw_ostream *OutputFile;
Daniel Dunbareef63e02011-02-02 15:41:17 +000021 unsigned CurrentIncludeDepth;
22 bool HasProcessedPredefines;
Daniel Dunbarb34d69b2011-02-02 21:11:31 +000023 bool OwnsOutputFile;
24 bool ShowAllHeaders;
Daniel Dunbarda608852011-03-21 19:37:38 +000025 bool ShowDepth;
Daniel Dunbareef63e02011-02-02 15:41:17 +000026
27public:
Daniel Dunbarb34d69b2011-02-02 21:11:31 +000028 HeaderIncludesCallback(const Preprocessor *PP, bool ShowAllHeaders_,
Daniel Dunbarda608852011-03-21 19:37:38 +000029 llvm::raw_ostream *OutputFile_, bool OwnsOutputFile_,
30 bool ShowDepth_)
Daniel Dunbarb34d69b2011-02-02 21:11:31 +000031 : SM(PP->getSourceManager()), OutputFile(OutputFile_),
32 CurrentIncludeDepth(0), HasProcessedPredefines(false),
Daniel Dunbarda608852011-03-21 19:37:38 +000033 OwnsOutputFile(OwnsOutputFile_), ShowAllHeaders(ShowAllHeaders_),
34 ShowDepth(ShowDepth_) {}
Daniel Dunbarb34d69b2011-02-02 21:11:31 +000035
36 ~HeaderIncludesCallback() {
37 if (OwnsOutputFile)
Daniel Dunbarf704c612011-02-03 03:45:00 +000038 delete OutputFile;
Daniel Dunbarb34d69b2011-02-02 21:11:31 +000039 }
Daniel Dunbareef63e02011-02-02 15:41:17 +000040
41 virtual void FileChanged(SourceLocation Loc, FileChangeReason Reason,
42 SrcMgr::CharacteristicKind FileType);
43};
44}
45
Daniel Dunbarb376e5e2011-02-02 21:11:24 +000046void clang::AttachHeaderIncludeGen(Preprocessor &PP, bool ShowAllHeaders,
Daniel Dunbarda608852011-03-21 19:37:38 +000047 llvm::StringRef OutputPath, bool ShowDepth) {
Daniel Dunbarf704c612011-02-03 03:45:00 +000048 llvm::raw_ostream *OutputFile = &llvm::errs();
49 bool OwnsOutputFile = false;
Daniel Dunbarb34d69b2011-02-02 21:11:31 +000050
51 // Open the output file, if used.
Daniel Dunbarf704c612011-02-03 03:45:00 +000052 if (!OutputPath.empty()) {
53 std::string Error;
54 llvm::raw_fd_ostream *OS = new llvm::raw_fd_ostream(
55 OutputPath.str().c_str(), Error, llvm::raw_fd_ostream::F_Append);
56 if (!Error.empty()) {
57 PP.getDiagnostics().Report(
58 clang::diag::warn_fe_cc_print_header_failure) << Error;
59 delete OS;
60 } else {
61 OS->SetUnbuffered();
62 OS->SetUseAtomicWrites(true);
63 OutputFile = OS;
64 OwnsOutputFile = true;
65 }
Daniel Dunbarb34d69b2011-02-02 21:11:31 +000066 }
67
68 PP.addPPCallbacks(new HeaderIncludesCallback(&PP, ShowAllHeaders,
Daniel Dunbarda608852011-03-21 19:37:38 +000069 OutputFile, OwnsOutputFile,
70 ShowDepth));
Daniel Dunbareef63e02011-02-02 15:41:17 +000071}
72
73void HeaderIncludesCallback::FileChanged(SourceLocation Loc,
74 FileChangeReason Reason,
75 SrcMgr::CharacteristicKind NewFileType) {
76 // Unless we are exiting a #include, make sure to skip ahead to the line the
77 // #include directive was at.
78 PresumedLoc UserLoc = SM.getPresumedLoc(Loc);
79 if (UserLoc.isInvalid())
80 return;
Daniel Dunbarb34d69b2011-02-02 21:11:31 +000081
Daniel Dunbareef63e02011-02-02 15:41:17 +000082 // Adjust the current include depth.
83 if (Reason == PPCallbacks::EnterFile) {
84 ++CurrentIncludeDepth;
Sebastian Redl4dddebf2011-04-14 14:07:45 +000085 } else if (Reason == PPCallbacks::ExitFile) {
Daniel Dunbareef63e02011-02-02 15:41:17 +000086 if (CurrentIncludeDepth)
87 --CurrentIncludeDepth;
88
89 // We track when we are done with the predefines by watching for the first
Sebastian Redl4dddebf2011-04-14 14:07:45 +000090 // place where we drop back to a nesting depth of 1.
91 if (CurrentIncludeDepth == 1 && !HasProcessedPredefines)
Daniel Dunbareef63e02011-02-02 15:41:17 +000092 HasProcessedPredefines = true;
Sebastian Redl4dddebf2011-04-14 14:07:45 +000093
94 return;
95 } else
96 return;
Daniel Dunbarb376e5e2011-02-02 21:11:24 +000097
98 // Show the header if we are (a) past the predefines, or (b) showing all
99 // headers and in the predefines at a depth past the initial file and command
100 // line buffers.
101 bool ShowHeader = (HasProcessedPredefines ||
102 (ShowAllHeaders && CurrentIncludeDepth > 2));
103
104 // Dump the header include information we are past the predefines buffer or
105 // are showing all headers.
106 if (ShowHeader && Reason == PPCallbacks::EnterFile) {
Daniel Dunbareef63e02011-02-02 15:41:17 +0000107 // Write to a temporary string to avoid unnecessary flushing on errs().
108 llvm::SmallString<512> Filename(UserLoc.getFilename());
109 Lexer::Stringify(Filename);
110
111 llvm::SmallString<256> Msg;
Daniel Dunbarda608852011-03-21 19:37:38 +0000112 if (ShowDepth) {
Sebastian Redl4dddebf2011-04-14 14:07:45 +0000113 // The main source file is at depth 1, so skip one dot.
114 for (unsigned i = 1; i != CurrentIncludeDepth; ++i)
Daniel Dunbarda608852011-03-21 19:37:38 +0000115 Msg += '.';
116 Msg += ' ';
117 }
Daniel Dunbareef63e02011-02-02 15:41:17 +0000118 Msg += Filename;
119 Msg += '\n';
120
Daniel Dunbarf704c612011-02-03 03:45:00 +0000121 OutputFile->write(Msg.data(), Msg.size());
Daniel Dunbareef63e02011-02-02 15:41:17 +0000122 }
123}