Skip to content

Commit b58a420

Browse files
committed
[Tooling/DependencyScanning] Rename refactorings towards transitioning dependency scanning to use pre-lexed preprocessor directive tokens
This is first of a series of patches for making the special lexing for dependency scanning a first-class feature of the `Preprocessor` and `Lexer`. This patch only includes NFC renaming changes to make reviewing of the functionality changing parts easier. Differential Revision: https://reviews.llvm.org/D125484
1 parent 628b2bf commit b58a420

24 files changed

+328
-289
lines changed

clang/include/clang/Basic/DiagnosticLexKinds.td

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -899,11 +899,11 @@ def err_pp_eof_in_assume_nonnull : Error<
899899

900900
}
901901

902-
let CategoryName = "Dependency Directive Source Minimization Issue" in {
902+
let CategoryName = "Dependency Directive Source Scanner Issue" in {
903903

904-
def err_dep_source_minimizer_missing_sema_after_at_import : Error<
904+
def err_dep_source_scanner_missing_semi_after_at_import : Error<
905905
"could not find ';' after @import">;
906-
def err_dep_source_minimizer_unexpected_tokens_at_import : Error<
906+
def err_dep_source_scanner_unexpected_tokens_at_import : Error<
907907
"unexpected extra tokens at end of @import declaration">;
908908

909909
}

clang/include/clang/Lex/DependencyDirectivesSourceMinimizer.h renamed to clang/include/clang/Lex/DependencyDirectivesScanner.h

Lines changed: 18 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
//===- clang/Lex/DependencyDirectivesSourceMinimizer.h - ----------*- C++ -*-//
1+
//===- clang/Lex/DependencyDirectivesScanner.h ---------------------*- C++ -*-//
22
//
33
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
44
// See https://llvm.org/LICENSE.txt for license information.
@@ -7,15 +7,15 @@
77
//===----------------------------------------------------------------------===//
88
///
99
/// \file
10-
/// This is the interface for minimizing header and source files to the
10+
/// This is the interface for scanning header and source files to get the
1111
/// minimum necessary preprocessor directives for evaluating includes. It
1212
/// reduces the source down to #define, #include, #import, @import, and any
1313
/// conditional preprocessor logic that contains one of those.
1414
///
1515
//===----------------------------------------------------------------------===//
1616

17-
#ifndef LLVM_CLANG_LEX_DEPENDENCYDIRECTIVESSOURCEMINIMIZER_H
18-
#define LLVM_CLANG_LEX_DEPENDENCYDIRECTIVESSOURCEMINIMIZER_H
17+
#ifndef LLVM_CLANG_LEX_DEPENDENCYDIRECTIVESSCANNER_H
18+
#define LLVM_CLANG_LEX_DEPENDENCYDIRECTIVESSCANNER_H
1919

2020
#include "clang/Basic/SourceLocation.h"
2121
#include "llvm/ADT/ArrayRef.h"
@@ -26,11 +26,11 @@ namespace clang {
2626

2727
class DiagnosticsEngine;
2828

29-
namespace minimize_source_to_dependency_directives {
29+
namespace dependency_directives_scan {
3030

3131
/// Represents the kind of preprocessor directive or a module declaration that
32-
/// is tracked by the source minimizer in its token output.
33-
enum TokenKind {
32+
/// is tracked by the scanner in its token output.
33+
enum DirectiveKind : uint8_t {
3434
pp_none,
3535
pp_include,
3636
pp___include_macros,
@@ -58,17 +58,17 @@ enum TokenKind {
5858
pp_eof,
5959
};
6060

61-
/// Represents a simplified token that's lexed as part of the source
62-
/// minimization. It's used to track the location of various preprocessor
63-
/// directives that could potentially have an effect on the depedencies.
64-
struct Token {
61+
/// Represents a directive that's lexed as part of the dependency directives
62+
/// scanning. It's used to track various preprocessor directives that could
63+
/// potentially have an effect on the depedencies.
64+
struct Directive {
6565
/// The kind of token.
66-
TokenKind K = pp_none;
66+
DirectiveKind Kind = pp_none;
6767

6868
/// Offset into the output byte stream of where the directive begins.
6969
int Offset = -1;
7070

71-
Token(TokenKind K, int Offset) : K(K), Offset(Offset) {}
71+
Directive(DirectiveKind K, int Offset) : Kind(K), Offset(Offset) {}
7272
};
7373

7474
/// Simplified token range to track the range of a potentially skippable PP
@@ -86,10 +86,10 @@ struct SkippedRange {
8686
/// when skipping a directive like #if, #ifdef or #elsif.
8787
///
8888
/// \returns false on success, true on error.
89-
bool computeSkippedRanges(ArrayRef<Token> Input,
89+
bool computeSkippedRanges(ArrayRef<Directive> Input,
9090
llvm::SmallVectorImpl<SkippedRange> &Range);
9191

92-
} // end namespace minimize_source_to_dependency_directives
92+
} // end namespace dependency_directives_scan
9393

9494
/// Minimize the input down to the preprocessor directives that might have
9595
/// an effect on the dependencies for a compilation unit.
@@ -103,13 +103,12 @@ bool computeSkippedRanges(ArrayRef<Token> Input,
103103
/// \returns false on success, true on error. If the diagnostic engine is not
104104
/// null, an appropriate error is reported using the given input location
105105
/// with the offset that corresponds to the minimizer's current buffer offset.
106-
bool minimizeSourceToDependencyDirectives(
106+
bool scanSourceForDependencyDirectives(
107107
llvm::StringRef Input, llvm::SmallVectorImpl<char> &Output,
108-
llvm::SmallVectorImpl<minimize_source_to_dependency_directives::Token>
109-
&Tokens,
108+
llvm::SmallVectorImpl<dependency_directives_scan::Directive> &Directives,
110109
DiagnosticsEngine *Diags = nullptr,
111110
SourceLocation InputSourceLoc = SourceLocation());
112111

113112
} // end namespace clang
114113

115-
#endif // LLVM_CLANG_LEX_DEPENDENCYDIRECTIVESSOURCEMINIMIZER_H
114+
#endif // LLVM_CLANG_LEX_DEPENDENCYDIRECTIVESSCANNER_H

clang/include/clang/Tooling/DependencyScanning/DependencyScanningFilesystem.h

Lines changed: 28 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -28,14 +28,14 @@ struct CachedFileContents {
2828
CachedFileContents(std::unique_ptr<llvm::MemoryBuffer> Original)
2929
: Original(std::move(Original)), MinimizedAccess(nullptr) {}
3030

31-
/// Owning storage for the minimized contents.
31+
/// Owning storage for the original contents.
3232
std::unique_ptr<llvm::MemoryBuffer> Original;
3333

34-
/// The mutex that must be locked before mutating minimized contents.
34+
/// The mutex that must be locked before mutating directive tokens.
3535
std::mutex ValueLock;
3636
/// Owning storage for the minimized contents.
3737
std::unique_ptr<llvm::MemoryBuffer> MinimizedStorage;
38-
/// Accessor to the minimized contents that's atomic to avoid data races.
38+
/// Accessor to the directive tokens that's atomic to avoid data races.
3939
std::atomic<llvm::MemoryBuffer *> MinimizedAccess;
4040
/// Skipped range mapping of the minimized contents.
4141
/// This is initialized iff `MinimizedAccess != nullptr`.
@@ -46,8 +46,8 @@ struct CachedFileContents {
4646
/// the dependency scanning filesystem.
4747
///
4848
/// It represents one of the following:
49-
/// - opened file with original contents and a stat value,
50-
/// - opened file with original contents, minimized contents and a stat value,
49+
/// - opened file with contents and a stat value,
50+
/// - opened file with contents, directive tokens and a stat value,
5151
/// - directory entry with its stat value,
5252
/// - filesystem error.
5353
///
@@ -84,8 +84,9 @@ class CachedFileSystemEntry {
8484
return Contents->Original->getBuffer();
8585
}
8686

87-
/// \returns Minimized contents of the file.
88-
StringRef getMinimizedContents() const {
87+
/// \returns The scanned preprocessor directive tokens of the file that are
88+
/// used to speed up preprocessing, if available.
89+
StringRef getDirectiveTokens() const {
8990
assert(!isError() && "error");
9091
assert(!MaybeStat->isDirectory() && "not a file");
9192
assert(Contents && "contents not initialized");
@@ -119,8 +120,8 @@ class CachedFileSystemEntry {
119120
return Contents->PPSkippedRangeMapping;
120121
}
121122

122-
/// \returns The data structure holding both original and minimized contents.
123-
CachedFileContents *getContents() const {
123+
/// \returns The data structure holding both contents and directive tokens.
124+
CachedFileContents *getCachedContents() const {
124125
assert(!isError() && "error");
125126
assert(!isDirectory() && "not a file");
126127
return Contents;
@@ -145,7 +146,7 @@ class CachedFileSystemEntry {
145146
};
146147

147148
/// This class is a shared cache, that caches the 'stat' and 'open' calls to the
148-
/// underlying real file system. It distinguishes between minimized and original
149+
/// underlying real file system, and the scanned preprocessor directives of
149150
/// files.
150151
///
151152
/// It is sharded based on the hash of the key to reduce the lock contention for
@@ -210,8 +211,7 @@ class DependencyScanningFilesystemSharedCache {
210211
};
211212

212213
/// This class is a local cache, that caches the 'stat' and 'open' calls to the
213-
/// underlying real file system. It distinguishes between minimized and original
214-
/// files.
214+
/// underlying real file system.
215215
class DependencyScanningFilesystemLocalCache {
216216
llvm::StringMap<const CachedFileSystemEntry *, llvm::BumpPtrAllocator> Cache;
217217

@@ -234,9 +234,8 @@ class DependencyScanningFilesystemLocalCache {
234234
};
235235

236236
/// Reference to a CachedFileSystemEntry.
237-
/// If the underlying entry is an opened file, this wrapper returns the correct
238-
/// contents (original or minimized) and ensures consistency with file size
239-
/// reported by status.
237+
/// If the underlying entry is an opened file, this wrapper returns the file
238+
/// contents and the scanned preprocessor directives.
240239
class EntryRef {
241240
/// For entry that is an opened file, this bit signifies whether its contents
242241
/// are minimized.
@@ -270,8 +269,7 @@ class EntryRef {
270269
}
271270

272271
StringRef getContents() const {
273-
return Minimized ? Entry.getMinimizedContents()
274-
: Entry.getOriginalContents();
272+
return Minimized ? Entry.getDirectiveTokens() : Entry.getOriginalContents();
275273
}
276274

277275
const PreprocessorSkippedRangeMapping *getPPSkippedRangeMapping() const {
@@ -301,33 +299,33 @@ class DependencyScanningWorkerFilesystem : public llvm::vfs::ProxyFileSystem {
301299
llvm::ErrorOr<std::unique_ptr<llvm::vfs::File>>
302300
openFileForRead(const Twine &Path) override;
303301

304-
/// Disable minimization of the given file.
305-
void disableMinimization(StringRef Filename);
306-
/// Enable minimization of all files.
307-
void enableMinimizationOfAllFiles() { NotToBeMinimized.clear(); }
302+
/// Disable directives scanning of the given file.
303+
void disableDirectivesScanning(StringRef Filename);
304+
/// Enable directives scanning of all files.
305+
void enableDirectivesScanningOfAllFiles() { NotToBeScanned.clear(); }
308306

309307
private:
310-
/// Check whether the file should be minimized.
311-
bool shouldMinimize(StringRef Filename, llvm::sys::fs::UniqueID UID);
308+
/// Check whether the file should be scanned for preprocessor directives.
309+
bool shouldScanForDirectives(StringRef Filename, llvm::sys::fs::UniqueID UID);
312310

313311
/// Returns entry for the given filename.
314312
///
315313
/// Attempts to use the local and shared caches first, then falls back to
316314
/// using the underlying filesystem.
317315
llvm::ErrorOr<EntryRef>
318316
getOrCreateFileSystemEntry(StringRef Filename,
319-
bool DisableMinimization = false);
317+
bool DisableDirectivesScanning = false);
320318

321319
/// For a filename that's not yet associated with any entry in the caches,
322320
/// uses the underlying filesystem to either look up the entry based in the
323321
/// shared cache indexed by unique ID, or creates new entry from scratch.
324322
llvm::ErrorOr<const CachedFileSystemEntry &>
325323
computeAndStoreResult(StringRef Filename);
326324

327-
/// Minimizes the given entry if necessary and returns a wrapper object with
328-
/// reference semantics.
329-
EntryRef minimizeIfNecessary(const CachedFileSystemEntry &Entry,
330-
StringRef Filename, bool Disable);
325+
/// Scan for preprocessor directives for the given entry if necessary and
326+
/// returns a wrapper object with reference semantics.
327+
EntryRef scanForDirectivesIfNecessary(const CachedFileSystemEntry &Entry,
328+
StringRef Filename, bool Disable);
331329

332330
/// Represents a filesystem entry that has been stat-ed (and potentially read)
333331
/// and that's about to be inserted into the cache as `CachedFileSystemEntry`.
@@ -402,8 +400,8 @@ class DependencyScanningWorkerFilesystem : public llvm::vfs::ProxyFileSystem {
402400
/// excluded conditional directive skip mappings that are used by the
403401
/// currently active preprocessor.
404402
ExcludedPreprocessorDirectiveSkipMapping &PPSkipMappings;
405-
/// The set of files that should not be minimized.
406-
llvm::DenseSet<llvm::sys::fs::UniqueID> NotToBeMinimized;
403+
/// The set of files that should not be scanned for PP directives.
404+
llvm::DenseSet<llvm::sys::fs::UniqueID> NotToBeScanned;
407405
};
408406

409407
} // end namespace dependencies

clang/include/clang/Tooling/DependencyScanning/DependencyScanningService.h

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -19,15 +19,13 @@ namespace dependencies {
1919
/// dependencies.
2020
enum class ScanningMode {
2121
/// This mode is used to compute the dependencies by running the preprocessor
22-
/// over
23-
/// the unmodified source files.
22+
/// over the source files.
2423
CanonicalPreprocessing,
2524

2625
/// This mode is used to compute the dependencies by running the preprocessor
27-
/// over
28-
/// the source files that have been minimized to contents that might affect
29-
/// the dependencies.
30-
MinimizedSourcePreprocessing
26+
/// with special kind of lexing after scanning header and source files to get
27+
/// the minimum necessary preprocessor directives for evaluating includes.
28+
DependencyDirectivesScan,
3129
};
3230

3331
/// The format that is output by the dependency scanner.

clang/lib/Frontend/FrontendActions.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
#include "clang/Frontend/FrontendDiagnostic.h"
1919
#include "clang/Frontend/MultiplexConsumer.h"
2020
#include "clang/Frontend/Utils.h"
21-
#include "clang/Lex/DependencyDirectivesSourceMinimizer.h"
21+
#include "clang/Lex/DependencyDirectivesScanner.h"
2222
#include "clang/Lex/HeaderSearch.h"
2323
#include "clang/Lex/Preprocessor.h"
2424
#include "clang/Lex/PreprocessorOptions.h"
@@ -1158,9 +1158,9 @@ void PrintDependencyDirectivesSourceMinimizerAction::ExecuteAction() {
11581158
llvm::MemoryBufferRef FromFile = SM.getBufferOrFake(SM.getMainFileID());
11591159

11601160
llvm::SmallString<1024> Output;
1161-
llvm::SmallVector<minimize_source_to_dependency_directives::Token, 32> Toks;
1162-
if (minimizeSourceToDependencyDirectives(
1163-
FromFile.getBuffer(), Output, Toks, &CI.getDiagnostics(),
1161+
llvm::SmallVector<dependency_directives_scan::Directive, 32> Directives;
1162+
if (scanSourceForDependencyDirectives(
1163+
FromFile.getBuffer(), Output, Directives, &CI.getDiagnostics(),
11641164
SM.getLocForStartOfFile(SM.getMainFileID()))) {
11651165
assert(CI.getDiagnostics().hasErrorOccurred() &&
11661166
"no errors reported for failure");

clang/lib/Lex/CMakeLists.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
set(LLVM_LINK_COMPONENTS support)
44

55
add_clang_library(clangLex
6-
DependencyDirectivesSourceMinimizer.cpp
6+
DependencyDirectivesScanner.cpp
77
HeaderMap.cpp
88
HeaderSearch.cpp
99
InitHeaderSearch.cpp

0 commit comments

Comments
 (0)