• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 //===--- 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/Frontend/FrontendDiagnostic.h"
13 #include "clang/Lex/Preprocessor.h"
14 #include "llvm/ADT/SmallString.h"
15 #include "llvm/Support/raw_ostream.h"
16 using namespace clang;
17 
18 namespace {
19 class HeaderIncludesCallback : public PPCallbacks {
20   SourceManager &SM;
21   raw_ostream *OutputFile;
22   unsigned CurrentIncludeDepth;
23   bool HasProcessedPredefines;
24   bool OwnsOutputFile;
25   bool ShowAllHeaders;
26   bool ShowDepth;
27   bool MSStyle;
28 
29 public:
HeaderIncludesCallback(const Preprocessor * PP,bool ShowAllHeaders_,raw_ostream * OutputFile_,bool OwnsOutputFile_,bool ShowDepth_,bool MSStyle_)30   HeaderIncludesCallback(const Preprocessor *PP, bool ShowAllHeaders_,
31                          raw_ostream *OutputFile_, bool OwnsOutputFile_,
32                          bool ShowDepth_, bool MSStyle_)
33     : SM(PP->getSourceManager()), OutputFile(OutputFile_),
34       CurrentIncludeDepth(0), HasProcessedPredefines(false),
35       OwnsOutputFile(OwnsOutputFile_), ShowAllHeaders(ShowAllHeaders_),
36       ShowDepth(ShowDepth_), MSStyle(MSStyle_) {}
37 
~HeaderIncludesCallback()38   ~HeaderIncludesCallback() {
39     if (OwnsOutputFile)
40       delete OutputFile;
41   }
42 
43   void FileChanged(SourceLocation Loc, FileChangeReason Reason,
44                    SrcMgr::CharacteristicKind FileType,
45                    FileID PrevFID) override;
46 };
47 }
48 
AttachHeaderIncludeGen(Preprocessor & PP,bool ShowAllHeaders,StringRef OutputPath,bool ShowDepth,bool MSStyle)49 void clang::AttachHeaderIncludeGen(Preprocessor &PP, bool ShowAllHeaders,
50                                    StringRef OutputPath, bool ShowDepth,
51                                    bool MSStyle) {
52   raw_ostream *OutputFile = MSStyle ? &llvm::outs() : &llvm::errs();
53   bool OwnsOutputFile = false;
54 
55   // Open the output file, if used.
56   if (!OutputPath.empty()) {
57     std::string Error;
58     llvm::raw_fd_ostream *OS = new llvm::raw_fd_ostream(
59         OutputPath.str().c_str(), Error,
60         llvm::sys::fs::F_Append | llvm::sys::fs::F_Text);
61     if (!Error.empty()) {
62       PP.getDiagnostics().Report(
63         clang::diag::warn_fe_cc_print_header_failure) << Error;
64       delete OS;
65     } else {
66       OS->SetUnbuffered();
67       OS->SetUseAtomicWrites(true);
68       OutputFile = OS;
69       OwnsOutputFile = true;
70     }
71   }
72 
73   PP.addPPCallbacks(new HeaderIncludesCallback(&PP, ShowAllHeaders,
74                                                OutputFile, OwnsOutputFile,
75                                                ShowDepth, MSStyle));
76 }
77 
FileChanged(SourceLocation Loc,FileChangeReason Reason,SrcMgr::CharacteristicKind NewFileType,FileID PrevFID)78 void HeaderIncludesCallback::FileChanged(SourceLocation Loc,
79                                          FileChangeReason Reason,
80                                        SrcMgr::CharacteristicKind NewFileType,
81                                        FileID PrevFID) {
82   // Unless we are exiting a #include, make sure to skip ahead to the line the
83   // #include directive was at.
84   PresumedLoc UserLoc = SM.getPresumedLoc(Loc);
85   if (UserLoc.isInvalid())
86     return;
87 
88   // Adjust the current include depth.
89   if (Reason == PPCallbacks::EnterFile) {
90     ++CurrentIncludeDepth;
91   } else if (Reason == PPCallbacks::ExitFile) {
92     if (CurrentIncludeDepth)
93       --CurrentIncludeDepth;
94 
95     // We track when we are done with the predefines by watching for the first
96     // place where we drop back to a nesting depth of 1.
97     if (CurrentIncludeDepth == 1 && !HasProcessedPredefines)
98       HasProcessedPredefines = true;
99 
100     return;
101   } else
102     return;
103 
104   // Show the header if we are (a) past the predefines, or (b) showing all
105   // headers and in the predefines at a depth past the initial file and command
106   // line buffers.
107   bool ShowHeader = (HasProcessedPredefines ||
108                      (ShowAllHeaders && CurrentIncludeDepth > 2));
109 
110   // Dump the header include information we are past the predefines buffer or
111   // are showing all headers.
112   if (ShowHeader && Reason == PPCallbacks::EnterFile) {
113     // Write to a temporary string to avoid unnecessary flushing on errs().
114     SmallString<512> Filename(UserLoc.getFilename());
115     if (!MSStyle)
116       Lexer::Stringify(Filename);
117 
118     SmallString<256> Msg;
119     if (MSStyle)
120       Msg += "Note: including file:";
121 
122     if (ShowDepth) {
123       // The main source file is at depth 1, so skip one dot.
124       for (unsigned i = 1; i != CurrentIncludeDepth; ++i)
125         Msg += MSStyle ? ' ' : '.';
126 
127       if (!MSStyle)
128         Msg += ' ';
129     }
130     Msg += Filename;
131     Msg += '\n';
132 
133     OutputFile->write(Msg.data(), Msg.size());
134   }
135 }
136