clang 23.0.0git
Utils.h
Go to the documentation of this file.
1//===- Utils.h - Misc utilities for the front-end ---------------*- C++ -*-===//
2//
3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6//
7//===----------------------------------------------------------------------===//
8//
9// This header contains miscellaneous utilities for various front-end actions.
10//
11//===----------------------------------------------------------------------===//
12
13#ifndef LLVM_CLANG_FRONTEND_UTILS_H
14#define LLVM_CLANG_FRONTEND_UTILS_H
15
17#include "clang/Basic/LLVM.h"
20#include "llvm/ADT/ArrayRef.h"
21#include "llvm/ADT/IntrusiveRefCntPtr.h"
22#include "llvm/ADT/StringMap.h"
23#include "llvm/ADT/StringRef.h"
24#include "llvm/ADT/StringSet.h"
25#include "llvm/Support/FileCollector.h"
26#include "llvm/Support/VirtualFileSystem.h"
27#include <cstdint>
28#include <memory>
29#include <string>
30#include <system_error>
31#include <utility>
32#include <vector>
33
34namespace clang {
35
36class ASTReader;
41class FrontendOptions;
43class Preprocessor;
46class CodeGenOptions;
47
48/// InitializePreprocessor - Initialize the preprocessor getting it and the
49/// environment ready to process a single file.
51 const PCHContainerReader &PCHContainerRdr,
52 const FrontendOptions &FEOpts,
53 const CodeGenOptions &CodeGenOpts);
54
55/// DoPrintPreprocessedInput - Implement -E mode.
56void DoPrintPreprocessedInput(Preprocessor &PP, raw_ostream *OS,
57 const PreprocessorOutputOptions &Opts);
58
59/// An interface for collecting the dependencies of a compilation. Users should
60/// use \c attachToPreprocessor and \c attachToASTReader to get all of the
61/// dependencies.
62/// FIXME: Migrate DependencyGraphGen to use this interface.
64public:
65 virtual ~DependencyCollector();
66
67 virtual void attachToPreprocessor(Preprocessor &PP);
68 virtual void attachToASTReader(ASTReader &R);
69 ArrayRef<std::string> getDependencies() const { return Dependencies; }
70
71 /// Called when a new file is seen. Return true if \p Filename should be added
72 /// to the list of dependencies.
73 ///
74 /// The default implementation ignores <built-in> and system files.
75 virtual bool sawDependency(StringRef Filename, bool FromModule, bool IsSystem,
76 bool IsModuleFile, bool IsDirectModuleImport,
77 bool IsMissing);
78
79 /// Called when the end of the main file is reached.
80 virtual void finishedMainFile(DiagnosticsEngine &Diags) {}
81
82 /// Return true if system files should be passed to sawDependency().
83 virtual bool needSystemDependencies() { return false; }
84
85 /// Add a dependency \p Filename if it has not been seen before and
86 /// sawDependency() returns true.
87 virtual void maybeAddDependency(StringRef Filename, bool FromModule,
88 bool IsSystem, bool IsModuleFile,
89 bool IsDirectModuleImport, bool IsMissing);
90
91protected:
92 /// Return true if the filename was added to the list of dependencies, false
93 /// otherwise.
94 bool addDependency(StringRef Filename);
95
96private:
97 llvm::StringSet<> Seen;
98 std::vector<std::string> Dependencies;
99};
100
101/// Builds a dependency file when attached to a Preprocessor (for includes) and
102/// ASTReader (for module imports), and writes it out at the end of processing
103/// a source file. Users should attach to the ast reader whenever a module is
104/// loaded.
106public:
108
109 void attachToPreprocessor(Preprocessor &PP) override;
110
111 void finishedMainFile(DiagnosticsEngine &Diags) override;
112
113 bool needSystemDependencies() final { return IncludeSystemHeaders; }
114
115 bool sawDependency(StringRef Filename, bool FromModule, bool IsSystem,
116 bool IsModuleFile, bool IsDirectModuleImport,
117 bool IsMissing) final;
118
119protected:
120 void outputDependencyFile(llvm::raw_ostream &OS);
121
122private:
124
125 std::string OutputFile;
126 std::vector<std::string> Targets;
127 bool IncludeSystemHeaders;
128 bool PhonyTarget;
129 bool AddMissingHeaderDeps;
130 bool SeenMissingHeader;
131 ModuleFileDepsKind IncludeModuleFiles;
132 DependencyOutputFormat OutputFormat;
133 unsigned InputFileIndex;
134};
135
136/// Collects the dependencies for imported modules into a directory. Users
137/// should attach to the AST reader whenever a module is loaded.
139 std::string DestDir;
140 bool HasErrors = false;
141 llvm::StringSet<> Seen;
142 llvm::vfs::YAMLVFSWriter VFSWriter;
143 llvm::FileCollector::PathCanonicalizer Canonicalizer;
144
145 std::error_code copyToRoot(StringRef Src, StringRef Dst = {});
146
147public:
148 ModuleDependencyCollector(std::string DestDir,
150 : DestDir(std::move(DestDir)), Canonicalizer(std::move(VFS)) {}
152
153 StringRef getDest() { return DestDir; }
154 virtual bool insertSeen(StringRef Filename) { return Seen.insert(Filename).second; }
155 virtual void addFile(StringRef Filename, StringRef FileDst = {});
156
157 virtual void addFileMapping(StringRef VPath, StringRef RPath) {
158 VFSWriter.addFileMapping(VPath, RPath);
159 }
160
161 void attachToPreprocessor(Preprocessor &PP) override;
162 void attachToASTReader(ASTReader &R) override;
163
164 virtual void writeFileMap();
165 virtual bool hasErrors() { return HasErrors; }
166};
167
168/// AttachDependencyGraphGen - Create a dependency graph generator, and attach
169/// it to the given preprocessor.
170void AttachDependencyGraphGen(Preprocessor &PP, StringRef OutputFile,
171 StringRef SysRoot);
172
173/// AttachHeaderIncludeGen - Create a header include list generator, and attach
174/// it to the given preprocessor.
175///
176/// \param DepOpts - Options controlling the output.
177/// \param ShowAllHeaders - If true, show all header information instead of just
178/// headers following the predefines buffer. This is useful for making sure
179/// includes mentioned on the command line are also reported, but differs from
180/// the default behavior used by -H.
181/// \param OutputPath - If non-empty, a path to write the header include
182/// information to, instead of writing to stderr.
183/// \param ShowDepth - Whether to indent to show the nesting of the includes.
184/// \param MSStyle - Whether to print in cl.exe /showIncludes style.
185void AttachHeaderIncludeGen(Preprocessor &PP,
186 const DependencyOutputOptions &DepOpts,
187 bool ShowAllHeaders = false,
188 StringRef OutputPath = {},
189 bool ShowDepth = true, bool MSStyle = false);
190
191/// The ChainedIncludesSource class converts headers to chained PCHs in
192/// memory, mainly for testing.
196
197} // namespace clang
198
199#endif // LLVM_CLANG_FRONTEND_UTILS_H
Defines the Diagnostic-related interfaces.
Forward-declares and imports various common LLVM datatypes that clang wants to use unqualified.
Reads an AST files chain containing the contents of a translation unit.
Definition ASTReader.h:428
CodeGenOptions - Track various options which control how the code is optimized and passed to the back...
CompilerInstance - Helper class for managing a single instance of the Clang compiler.
Helper class for holding the data necessary to invoke the compiler.
An interface for collecting the dependencies of a compilation.
Definition Utils.h:63
bool addDependency(StringRef Filename)
Return true if the filename was added to the list of dependencies, false otherwise.
virtual void attachToPreprocessor(Preprocessor &PP)
virtual void maybeAddDependency(StringRef Filename, bool FromModule, bool IsSystem, bool IsModuleFile, bool IsDirectModuleImport, bool IsMissing)
Add a dependency Filename if it has not been seen before and sawDependency() returns true.
ArrayRef< std::string > getDependencies() const
Definition Utils.h:69
virtual void finishedMainFile(DiagnosticsEngine &Diags)
Called when the end of the main file is reached.
Definition Utils.h:80
virtual void attachToASTReader(ASTReader &R)
virtual bool sawDependency(StringRef Filename, bool FromModule, bool IsSystem, bool IsModuleFile, bool IsDirectModuleImport, bool IsMissing)
Called when a new file is seen.
virtual bool needSystemDependencies()
Return true if system files should be passed to sawDependency().
Definition Utils.h:83
void outputDependencyFile(llvm::raw_ostream &OS)
void attachToPreprocessor(Preprocessor &PP) override
void finishedMainFile(DiagnosticsEngine &Diags) override
Called when the end of the main file is reached.
bool needSystemDependencies() final
Return true if system files should be passed to sawDependency().
Definition Utils.h:113
bool sawDependency(StringRef Filename, bool FromModule, bool IsSystem, bool IsModuleFile, bool IsDirectModuleImport, bool IsMissing) final
Called when a new file is seen.
DependencyFileGenerator(const DependencyOutputOptions &Opts)
DependencyOutputOptions - Options for controlling the compiler dependency file generation.
Concrete class used by the front-end to report problems and issues.
Definition Diagnostic.h:233
An abstract interface that should be implemented by external AST sources that also provide informatio...
FrontendOptions - Options for controlling the behavior of the frontend.
void attachToASTReader(ASTReader &R) override
~ModuleDependencyCollector() override
Definition Utils.h:151
virtual void addFileMapping(StringRef VPath, StringRef RPath)
Definition Utils.h:157
ModuleDependencyCollector(std::string DestDir, IntrusiveRefCntPtr< llvm::vfs::FileSystem > VFS)
Definition Utils.h:148
void attachToPreprocessor(Preprocessor &PP) override
virtual void addFile(StringRef Filename, StringRef FileDst={})
virtual bool insertSeen(StringRef Filename)
Definition Utils.h:154
This abstract interface provides operations for unwrapping containers for serialized ASTs (precompile...
PreprocessorOptions - This class is used for passing the various options used in preprocessor initial...
PreprocessorOutputOptions - Options for controlling the C preprocessor output (e.g....
Engages in a tight little dance with the lexer to efficiently preprocess tokens.
The JSON file list parser is used to communicate input to InstallAPI.
void InitializePreprocessor(Preprocessor &PP, const PreprocessorOptions &PPOpts, const PCHContainerReader &PCHContainerRdr, const FrontendOptions &FEOpts, const CodeGenOptions &CodeGenOpts)
InitializePreprocessor - Initialize the preprocessor getting it and the environment ready to process ...
IntrusiveRefCntPtr< ExternalSemaSource > createChainedIncludesSource(CompilerInstance &CI, IntrusiveRefCntPtr< ASTReader > &OutReader)
The ChainedIncludesSource class converts headers to chained PCHs in memory, mainly for testing.
ModuleFileDepsKind
ModuleFileDepsKind - Whether to include module file dependencies.
void DoPrintPreprocessedInput(Preprocessor &PP, raw_ostream *OS, const PreprocessorOutputOptions &Opts)
DoPrintPreprocessedInput - Implement -E mode.
DependencyOutputFormat
DependencyOutputFormat - Format for the compiler dependency file.
void AttachHeaderIncludeGen(Preprocessor &PP, const DependencyOutputOptions &DepOpts, bool ShowAllHeaders=false, StringRef OutputPath={}, bool ShowDepth=true, bool MSStyle=false)
AttachHeaderIncludeGen - Create a header include list generator, and attach it to the given preproces...
void AttachDependencyGraphGen(Preprocessor &PP, StringRef OutputFile, StringRef SysRoot)
AttachDependencyGraphGen - Create a dependency graph generator, and attach it to the given preprocess...