blob: 480a97dd101d83fb5265395388e0be23c87fa308 [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 Dunbareef63e02011-02-02 15:41:17 +000013using namespace clang;
14
15namespace {
16class HeaderIncludesCallback : public PPCallbacks {
17 SourceManager &SM;
18 unsigned CurrentIncludeDepth;
Daniel Dunbarb376e5e2011-02-02 21:11:24 +000019 bool ShowAllHeaders;
Daniel Dunbareef63e02011-02-02 15:41:17 +000020 bool HasProcessedPredefines;
21
22public:
Daniel Dunbarb376e5e2011-02-02 21:11:24 +000023 HeaderIncludesCallback(const Preprocessor *PP, bool ShowAllHeaders_)
Daniel Dunbareef63e02011-02-02 15:41:17 +000024 : SM(PP->getSourceManager()), CurrentIncludeDepth(0),
Daniel Dunbarb376e5e2011-02-02 21:11:24 +000025 ShowAllHeaders(ShowAllHeaders_), HasProcessedPredefines(false) {}
Daniel Dunbareef63e02011-02-02 15:41:17 +000026
27 virtual void FileChanged(SourceLocation Loc, FileChangeReason Reason,
28 SrcMgr::CharacteristicKind FileType);
29};
30}
31
Daniel Dunbarb376e5e2011-02-02 21:11:24 +000032void clang::AttachHeaderIncludeGen(Preprocessor &PP, bool ShowAllHeaders,
33 llvm::StringRef OutputPath) {
34 PP.addPPCallbacks(new HeaderIncludesCallback(&PP, ShowAllHeaders));
Daniel Dunbareef63e02011-02-02 15:41:17 +000035}
36
37void HeaderIncludesCallback::FileChanged(SourceLocation Loc,
38 FileChangeReason Reason,
39 SrcMgr::CharacteristicKind NewFileType) {
40 // Unless we are exiting a #include, make sure to skip ahead to the line the
41 // #include directive was at.
42 PresumedLoc UserLoc = SM.getPresumedLoc(Loc);
43 if (UserLoc.isInvalid())
44 return;
45
46 // Adjust the current include depth.
47 if (Reason == PPCallbacks::EnterFile) {
48 ++CurrentIncludeDepth;
49 } else {
50 if (CurrentIncludeDepth)
51 --CurrentIncludeDepth;
52
53 // We track when we are done with the predefines by watching for the first
54 // place where we drop back to a nesting depth of 0.
55 if (CurrentIncludeDepth == 0 && !HasProcessedPredefines)
56 HasProcessedPredefines = true;
57 }
Daniel Dunbarb376e5e2011-02-02 21:11:24 +000058
59 // Show the header if we are (a) past the predefines, or (b) showing all
60 // headers and in the predefines at a depth past the initial file and command
61 // line buffers.
62 bool ShowHeader = (HasProcessedPredefines ||
63 (ShowAllHeaders && CurrentIncludeDepth > 2));
64
65 // Dump the header include information we are past the predefines buffer or
66 // are showing all headers.
67 if (ShowHeader && Reason == PPCallbacks::EnterFile) {
Daniel Dunbareef63e02011-02-02 15:41:17 +000068 // Write to a temporary string to avoid unnecessary flushing on errs().
69 llvm::SmallString<512> Filename(UserLoc.getFilename());
70 Lexer::Stringify(Filename);
71
72 llvm::SmallString<256> Msg;
73 for (unsigned i = 0; i != CurrentIncludeDepth; ++i)
74 Msg += '.';
75 Msg += ' ';
76 Msg += Filename;
77 Msg += '\n';
78
79 llvm::errs() << Msg;
80 }
81}
82