clang 22.0.0git
DependencyDirectivesScanner.h
Go to the documentation of this file.
1//===- clang/Lex/DependencyDirectivesScanner.h ---------------------*- 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/// \file
10/// This is the interface for scanning header and source files to get the
11/// minimum necessary preprocessor directives for evaluating includes. It
12/// reduces the source down to #define, #include, #import, @import, and any
13/// conditional preprocessor logic that contains one of those.
14///
15//===----------------------------------------------------------------------===//
16
17#ifndef LLVM_CLANG_LEX_DEPENDENCYDIRECTIVESSCANNER_H
18#define LLVM_CLANG_LEX_DEPENDENCYDIRECTIVESSCANNER_H
19
21#include "llvm/ADT/ArrayRef.h"
22
23namespace clang {
24class FileManager;
25
26namespace tok {
27enum TokenKind : unsigned short;
28}
29
30class DiagnosticsEngine;
31
32namespace dependency_directives_scan {
33
34/// Token lexed as part of dependency directive scanning.
35struct Token {
36 /// Offset into the original source input.
37 unsigned Offset;
38 unsigned Length;
40 unsigned short Flags;
41
42 Token(unsigned Offset, unsigned Length, tok::TokenKind Kind,
43 unsigned short Flags)
45
46 unsigned getEnd() const { return Offset + Length; }
47
48 bool is(tok::TokenKind K) const { return Kind == K; }
49 bool isNot(tok::TokenKind K) const { return Kind != K; }
50 template <typename... Ts> bool isOneOf(Ts... Ks) const {
51 static_assert(sizeof...(Ts) > 0,
52 "requires at least one tok::TokenKind specified");
53 return (is(Ks) || ...);
54 }
55};
56
57/// Represents the kind of preprocessor directive or a module declaration that
58/// is tracked by the scanner in its token output.
59enum DirectiveKind : uint8_t {
86 /// Indicates that there are tokens present between the last scanned directive
87 /// and eof. The \p Directive::Tokens array will be empty for this kind.
90};
91
92/// Represents a directive that's lexed as part of the dependency directives
93/// scanning. It's used to track various preprocessor directives that could
94/// potentially have an effect on the dependencies.
95struct Directive {
97
98 /// The kind of token.
100
101 Directive() = default;
103 : Tokens(Tokens), Kind(K) {}
104};
105
106} // end namespace dependency_directives_scan
107
108/// Scan the input for the preprocessor directives that might have
109/// an effect on the dependencies for a compilation unit.
110///
111/// This function ignores all non-preprocessor code and anything that
112/// can't affect what gets included.
113///
114/// \returns false on success, true on error. If the diagnostic engine is not
115/// null, an appropriate error is reported using the given input location
116/// with the offset that corresponds to the \p Input buffer offset.
118 StringRef Input, SmallVectorImpl<dependency_directives_scan::Token> &Tokens,
119 SmallVectorImpl<dependency_directives_scan::Directive> &Directives,
120 DiagnosticsEngine *Diags = nullptr,
121 SourceLocation InputSourceLoc = SourceLocation());
122
123/// Print the previously scanned dependency directives as minimized source text.
124///
125/// \param Source The original source text that the dependency directives were
126/// scanned from.
127/// \param Directives The previously scanned dependency
128/// directives.
129/// \param OS the stream to print the dependency directives on.
130///
131/// This is used primarily for testing purposes, during dependency scanning the
132/// \p Lexer uses the tokens directly, not their printed version.
134 StringRef Source,
135 ArrayRef<dependency_directives_scan::Directive> Directives,
136 llvm::raw_ostream &OS);
137
138/// Scan an input source buffer for C++20 named module usage.
139///
140/// \param Source The input source buffer.
141///
142/// \returns true if any C++20 named modules related directive was found.
143bool scanInputForCXX20ModulesUsage(StringRef Source);
144
145/// Functor that returns the dependency directives for a given file.
147public:
148 /// Clone the getter for a new \c FileManager instance.
149 virtual std::unique_ptr<DependencyDirectivesGetter>
150 cloneFor(FileManager &FileMgr) = 0;
151
152 /// Get the dependency directives for the given file.
153 virtual std::optional<ArrayRef<dependency_directives_scan::Directive>>
155
156 virtual ~DependencyDirectivesGetter() = default;
157};
158} // end namespace clang
159
160#endif // LLVM_CLANG_LEX_DEPENDENCYDIRECTIVESSCANNER_H
Defines the clang::SourceLocation class and associated facilities.
Functor that returns the dependency directives for a given file.
virtual std::optional< ArrayRef< dependency_directives_scan::Directive > > operator()(FileEntryRef File)=0
Get the dependency directives for the given file.
virtual ~DependencyDirectivesGetter()=default
virtual std::unique_ptr< DependencyDirectivesGetter > cloneFor(FileManager &FileMgr)=0
Clone the getter for a new FileManager instance.
A reference to a FileEntry that includes the name of the file as it was accessed by the FileManager's...
Definition: FileEntry.h:57
Implements support for file system lookup, file system caching, and directory search management.
Definition: FileManager.h:53
DirectiveKind
Represents the kind of preprocessor directive or a module declaration that is tracked by the scanner ...
@ tokens_present_before_eof
Indicates that there are tokens present between the last scanned directive and eof.
TokenKind
Provides a simple uniform namespace for tokens from all C languages.
Definition: TokenKinds.h:25
The JSON file list parser is used to communicate input to InstallAPI.
void printDependencyDirectivesAsSource(StringRef Source, ArrayRef< dependency_directives_scan::Directive > Directives, llvm::raw_ostream &OS)
Print the previously scanned dependency directives as minimized source text.
bool scanInputForCXX20ModulesUsage(StringRef Source)
Scan an input source buffer for C++20 named module usage.
bool scanSourceForDependencyDirectives(StringRef Input, SmallVectorImpl< dependency_directives_scan::Token > &Tokens, SmallVectorImpl< dependency_directives_scan::Directive > &Directives, DiagnosticsEngine *Diags=nullptr, SourceLocation InputSourceLoc=SourceLocation())
Scan the input for the preprocessor directives that might have an effect on the dependencies for a co...
Represents a directive that's lexed as part of the dependency directives scanning.
Directive(DirectiveKind K, ArrayRef< Token > Tokens)
Token lexed as part of dependency directive scanning.
Token(unsigned Offset, unsigned Length, tok::TokenKind Kind, unsigned short Flags)
unsigned Offset
Offset into the original source input.