clang  3.7.0
HeaderIncludeGen.cpp
Go to the documentation of this file.
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"
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 {
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:
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 
38  ~HeaderIncludesCallback() override {
39  if (OwnsOutputFile)
40  delete OutputFile;
41  }
42 
43  void FileChanged(SourceLocation Loc, FileChangeReason Reason,
45  FileID PrevFID) override;
46 };
47 }
48 
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::error_code EC;
58  llvm::raw_fd_ostream *OS = new llvm::raw_fd_ostream(
59  OutputPath.str(), EC, llvm::sys::fs::F_Append | llvm::sys::fs::F_Text);
60  if (EC) {
61  PP.getDiagnostics().Report(clang::diag::warn_fe_cc_print_header_failure)
62  << EC.message();
63  delete OS;
64  } else {
65  OS->SetUnbuffered();
66  OS->SetUseAtomicWrites(true);
67  OutputFile = OS;
68  OwnsOutputFile = true;
69  }
70  }
71 
72  PP.addPPCallbacks(llvm::make_unique<HeaderIncludesCallback>(&PP,
73  ShowAllHeaders,
74  OutputFile,
75  OwnsOutputFile,
76  ShowDepth,
77  MSStyle));
78 }
79 
80 void HeaderIncludesCallback::FileChanged(SourceLocation Loc,
81  FileChangeReason Reason,
82  SrcMgr::CharacteristicKind NewFileType,
83  FileID PrevFID) {
84  // Unless we are exiting a #include, make sure to skip ahead to the line the
85  // #include directive was at.
86  PresumedLoc UserLoc = SM.getPresumedLoc(Loc);
87  if (UserLoc.isInvalid())
88  return;
89 
90  // Adjust the current include depth.
91  if (Reason == PPCallbacks::EnterFile) {
92  ++CurrentIncludeDepth;
93  } else if (Reason == PPCallbacks::ExitFile) {
94  if (CurrentIncludeDepth)
95  --CurrentIncludeDepth;
96 
97  // We track when we are done with the predefines by watching for the first
98  // place where we drop back to a nesting depth of 1.
99  if (CurrentIncludeDepth == 1 && !HasProcessedPredefines)
100  HasProcessedPredefines = true;
101 
102  return;
103  } else
104  return;
105 
106  // Show the header if we are (a) past the predefines, or (b) showing all
107  // headers and in the predefines at a depth past the initial file and command
108  // line buffers.
109  bool ShowHeader = (HasProcessedPredefines ||
110  (ShowAllHeaders && CurrentIncludeDepth > 2));
111 
112  // Dump the header include information we are past the predefines buffer or
113  // are showing all headers.
114  if (ShowHeader && Reason == PPCallbacks::EnterFile) {
115  // Write to a temporary string to avoid unnecessary flushing on errs().
116  SmallString<512> Filename(UserLoc.getFilename());
117  if (!MSStyle)
118  Lexer::Stringify(Filename);
119 
120  SmallString<256> Msg;
121  if (MSStyle)
122  Msg += "Note: including file:";
123 
124  if (ShowDepth) {
125  // The main source file is at depth 1, so skip one dot.
126  for (unsigned i = 1; i != CurrentIncludeDepth; ++i)
127  Msg += MSStyle ? ' ' : '.';
128 
129  if (!MSStyle)
130  Msg += ' ';
131  }
132  Msg += Filename;
133  Msg += '\n';
134 
135  OutputFile->write(Msg.data(), Msg.size());
136  OutputFile->flush();
137  }
138 }
void AttachHeaderIncludeGen(Preprocessor &PP, bool ShowAllHeaders=false, StringRef OutputPath="", bool ShowDepth=true, bool MSStyle=false)
bool isInvalid() const
Return true if this object is invalid or uninitialized.
Defines the SourceManager interface.
DiagnosticBuilder Report(SourceLocation Loc, unsigned DiagID)
Issue the message to the client.
Definition: Diagnostic.h:1118
CharacteristicKind
Indicates whether a file or directory holds normal user code, system code, or system code which is im...
Definition: SourceManager.h:78
This interface provides a way to observe the actions of the preprocessor as it does its thing...
Definition: PPCallbacks.h:38
SourceManager & SM
Defines the clang::Preprocessor interface.
Represents an unpacked "presumed" location which can be presented to the user.
#define false
Definition: stdbool.h:33
const char * getFilename() const
Return the presumed filename of this location.
Encodes a location in the source. The SourceManager can decode this to get at the full include stack...
DiagnosticsEngine & getDiagnostics() const
Definition: Preprocessor.h:676
An opaque identifier used by SourceManager which refers to a source file (MemoryBuffer) along with it...
void addPPCallbacks(std::unique_ptr< PPCallbacks > C)
Definition: Preprocessor.h:773
This class handles loading and caching of source files into memory.
static std::string Stringify(StringRef Str, bool Charify=false)
Definition: Lexer.cpp:202
Engages in a tight little dance with the lexer to efficiently preprocess tokens.
Definition: Preprocessor.h:96
PresumedLoc getPresumedLoc(SourceLocation Loc, bool UseLineDirectives=true) const
Returns the "presumed" location of a SourceLocation specifies.