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