blob: 0d478d7065bfec167f9073b174d2e65ad3987b16 [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"
12#include "clang/Lex/Preprocessor.h"
Daniel Dunbarb34d69b2011-02-02 21:11:31 +000013#include <cstdio>
Daniel Dunbareef63e02011-02-02 15:41:17 +000014using namespace clang;
15
16namespace {
17class HeaderIncludesCallback : public PPCallbacks {
18 SourceManager &SM;
Daniel Dunbarb34d69b2011-02-02 21:11:31 +000019 FILE *OutputFile;
Daniel Dunbareef63e02011-02-02 15:41:17 +000020 unsigned CurrentIncludeDepth;
21 bool HasProcessedPredefines;
Daniel Dunbarb34d69b2011-02-02 21:11:31 +000022 bool OwnsOutputFile;
23 bool ShowAllHeaders;
Daniel Dunbareef63e02011-02-02 15:41:17 +000024
25public:
Daniel Dunbarb34d69b2011-02-02 21:11:31 +000026 HeaderIncludesCallback(const Preprocessor *PP, bool ShowAllHeaders_,
27 FILE *OutputFile_, bool OwnsOutputFile_)
28 : SM(PP->getSourceManager()), OutputFile(OutputFile_),
29 CurrentIncludeDepth(0), HasProcessedPredefines(false),
30 OwnsOutputFile(OwnsOutputFile_), ShowAllHeaders(ShowAllHeaders_) {}
31
32 ~HeaderIncludesCallback() {
33 if (OwnsOutputFile)
34 fclose(OutputFile);
35 }
Daniel Dunbareef63e02011-02-02 15:41:17 +000036
37 virtual void FileChanged(SourceLocation Loc, FileChangeReason Reason,
38 SrcMgr::CharacteristicKind FileType);
39};
40}
41
Daniel Dunbarb376e5e2011-02-02 21:11:24 +000042void clang::AttachHeaderIncludeGen(Preprocessor &PP, bool ShowAllHeaders,
43 llvm::StringRef OutputPath) {
Daniel Dunbarb34d69b2011-02-02 21:11:31 +000044 FILE *OutputFile;
45 bool OwnsOutputFile;
46
47 // Open the output file, if used.
48 if (OutputPath.empty()) {
49 OutputFile = stderr;
50 OwnsOutputFile = false;
51 } else {
52 OutputFile = fopen(OutputPath.str().c_str(), "a");
53 OwnsOutputFile = true;
54 }
55
56 PP.addPPCallbacks(new HeaderIncludesCallback(&PP, ShowAllHeaders,
57 OutputFile, OwnsOutputFile));
Daniel Dunbareef63e02011-02-02 15:41:17 +000058}
59
60void HeaderIncludesCallback::FileChanged(SourceLocation Loc,
61 FileChangeReason Reason,
62 SrcMgr::CharacteristicKind NewFileType) {
63 // Unless we are exiting a #include, make sure to skip ahead to the line the
64 // #include directive was at.
65 PresumedLoc UserLoc = SM.getPresumedLoc(Loc);
66 if (UserLoc.isInvalid())
67 return;
Daniel Dunbarb34d69b2011-02-02 21:11:31 +000068
Daniel Dunbareef63e02011-02-02 15:41:17 +000069 // Adjust the current include depth.
70 if (Reason == PPCallbacks::EnterFile) {
71 ++CurrentIncludeDepth;
72 } else {
73 if (CurrentIncludeDepth)
74 --CurrentIncludeDepth;
75
76 // We track when we are done with the predefines by watching for the first
77 // place where we drop back to a nesting depth of 0.
78 if (CurrentIncludeDepth == 0 && !HasProcessedPredefines)
79 HasProcessedPredefines = true;
80 }
Daniel Dunbarb376e5e2011-02-02 21:11:24 +000081
82 // Show the header if we are (a) past the predefines, or (b) showing all
83 // headers and in the predefines at a depth past the initial file and command
84 // line buffers.
85 bool ShowHeader = (HasProcessedPredefines ||
86 (ShowAllHeaders && CurrentIncludeDepth > 2));
87
88 // Dump the header include information we are past the predefines buffer or
89 // are showing all headers.
90 if (ShowHeader && Reason == PPCallbacks::EnterFile) {
Daniel Dunbareef63e02011-02-02 15:41:17 +000091 // Write to a temporary string to avoid unnecessary flushing on errs().
92 llvm::SmallString<512> Filename(UserLoc.getFilename());
93 Lexer::Stringify(Filename);
94
95 llvm::SmallString<256> Msg;
96 for (unsigned i = 0; i != CurrentIncludeDepth; ++i)
97 Msg += '.';
98 Msg += ' ';
99 Msg += Filename;
100 Msg += '\n';
101
Daniel Dunbarb34d69b2011-02-02 21:11:31 +0000102 fwrite(Msg.data(), Msg.size(), 1, OutputFile);
Daniel Dunbareef63e02011-02-02 15:41:17 +0000103 }
104}
105