Skip to content

Commit eca53ab

Browse files
authored
Merge pull request #3441 from apple/jan_svoboda/cherry_pick
[clang][deps] Cherry-pick some dependency-scanner related commits
2 parents 77a5a49 + 6029b46 commit eca53ab

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

66 files changed

+895
-209
lines changed

clang/include/clang/Basic/DiagnosticGroups.td

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -744,6 +744,7 @@ def UnusedLocalTypedef : DiagGroup<"unused-local-typedef">;
744744
def UnusedPropertyIvar : DiagGroup<"unused-property-ivar">;
745745
def UnusedGetterReturnValue : DiagGroup<"unused-getter-return-value">;
746746
def UsedButMarkedUnused : DiagGroup<"used-but-marked-unused">;
747+
def UsedSearchPath : DiagGroup<"search-path-usage">;
747748
def UserDefinedLiterals : DiagGroup<"user-defined-literals">;
748749
def UserDefinedWarnings : DiagGroup<"user-defined-warnings">;
749750
def ReorderCtor : DiagGroup<"reorder-ctor">;

clang/include/clang/Basic/DiagnosticLexKinds.td

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -430,6 +430,9 @@ def warn_pp_hdrstop_filename_ignored : Warning<
430430
"#pragma hdrstop filename not supported, "
431431
"/Fp can be used to specify precompiled header filename">,
432432
InGroup<ClangClPch>;
433+
def remark_pp_search_path_usage : Remark<
434+
"search path used: '%0'">,
435+
InGroup<UsedSearchPath>;
433436
def err_pp_file_not_found_angled_include_not_fatal : Error<
434437
"'%0' file not found with <angled> %select{include|import}1; "
435438
"use \"quotes\" instead">;

clang/include/clang/Lex/HeaderMap.h

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -77,13 +77,6 @@ class HeaderMap : private HeaderMapImpl {
7777
static std::unique_ptr<HeaderMap> Create(const FileEntry *FE,
7878
FileManager &FM);
7979

80-
/// Check to see if the specified relative filename is located in this
81-
/// HeaderMap. If so, open it and return its FileEntry. If RawPath is not
82-
/// NULL and the file is found, RawPath will be set to the raw path at which
83-
/// the file was found in the file system. For example, for a search path
84-
/// ".." and a filename "../file.h" this would be "../../file.h".
85-
Optional<FileEntryRef> LookupFile(StringRef Filename, FileManager &FM) const;
86-
8780
using HeaderMapImpl::dump;
8881
using HeaderMapImpl::getFileName;
8982
using HeaderMapImpl::lookupFilename;

clang/include/clang/Lex/HeaderSearch.h

Lines changed: 36 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -172,6 +172,9 @@ class HeaderSearch {
172172
/// Header-search options used to initialize this header search.
173173
std::shared_ptr<HeaderSearchOptions> HSOpts;
174174

175+
/// Mapping from SearchDir to HeaderSearchOptions::UserEntries indices.
176+
llvm::DenseMap<unsigned, unsigned> SearchDirToHSEntry;
177+
175178
DiagnosticsEngine &Diags;
176179
FileManager &FileMgr;
177180

@@ -182,6 +185,9 @@ class HeaderSearch {
182185
/// NoCurDirSearch is true, then the check for the file in the current
183186
/// directory is suppressed.
184187
std::vector<DirectoryLookup> SearchDirs;
188+
/// Whether the DirectoryLookup at the corresponding index in SearchDirs has
189+
/// been successfully used to lookup a file.
190+
std::vector<bool> SearchDirsUsage;
185191
unsigned AngledDirIdx = 0;
186192
unsigned SystemDirIdx = 0;
187193
bool NoCurDirSearch = false;
@@ -280,22 +286,25 @@ class HeaderSearch {
280286
DiagnosticsEngine &getDiags() const { return Diags; }
281287

282288
/// Interface for setting the file search paths.
283-
void SetSearchPaths(const std::vector<DirectoryLookup> &dirs,
284-
unsigned angledDirIdx, unsigned systemDirIdx,
285-
bool noCurDirSearch) {
289+
void SetSearchPaths(std::vector<DirectoryLookup> dirs, unsigned angledDirIdx,
290+
unsigned systemDirIdx, bool noCurDirSearch,
291+
llvm::DenseMap<unsigned, unsigned> searchDirToHSEntry) {
286292
assert(angledDirIdx <= systemDirIdx && systemDirIdx <= dirs.size() &&
287293
"Directory indices are unordered");
288-
SearchDirs = dirs;
294+
SearchDirs = std::move(dirs);
295+
SearchDirsUsage.assign(SearchDirs.size(), false);
289296
AngledDirIdx = angledDirIdx;
290297
SystemDirIdx = systemDirIdx;
291298
NoCurDirSearch = noCurDirSearch;
299+
SearchDirToHSEntry = std::move(searchDirToHSEntry);
292300
//LookupFileCache.clear();
293301
}
294302

295303
/// Add an additional search path.
296304
void AddSearchPath(const DirectoryLookup &dir, bool isAngled) {
297305
unsigned idx = isAngled ? SystemDirIdx : AngledDirIdx;
298306
SearchDirs.insert(SearchDirs.begin() + idx, dir);
307+
SearchDirsUsage.insert(SearchDirsUsage.begin() + idx, false);
299308
if (!isAngled)
300309
AngledDirIdx++;
301310
SystemDirIdx++;
@@ -507,6 +516,10 @@ class HeaderSearch {
507516
return FI && FI->isImport;
508517
}
509518

519+
/// Determine which HeaderSearchOptions::UserEntries have been successfully
520+
/// used so far and mark their index with 'true' in the resulting bit vector.
521+
std::vector<bool> computeUserEntryUsage() const;
522+
510523
/// This method returns a HeaderMap for the specified
511524
/// FileEntry, uniquing them through the 'HeaderMaps' datastructure.
512525
const HeaderMap *CreateHeaderMap(const FileEntry *FE);
@@ -562,6 +575,8 @@ class HeaderSearch {
562575
///
563576
/// \param ModuleName The name of the module we're looking for.
564577
///
578+
/// \param ImportLoc Location of the module include/import.
579+
///
565580
/// \param AllowSearch Whether we are allowed to search in the various
566581
/// search directories to produce a module definition. If not, this lookup
567582
/// will only return an already-known module.
@@ -570,7 +585,9 @@ class HeaderSearch {
570585
/// in subdirectories.
571586
///
572587
/// \returns The module with the given name.
573-
Module *lookupModule(StringRef ModuleName, bool AllowSearch = true,
588+
Module *lookupModule(StringRef ModuleName,
589+
SourceLocation ImportLoc = SourceLocation(),
590+
bool AllowSearch = true,
574591
bool AllowExtraModuleMapSearch = false);
575592

576593
/// Try to find a module map file in the given directory, returning
@@ -640,11 +657,14 @@ class HeaderSearch {
640657
/// but for compatibility with some buggy frameworks, additional attempts
641658
/// may be made to find the module under a related-but-different search-name.
642659
///
660+
/// \param ImportLoc Location of the module include/import.
661+
///
643662
/// \param AllowExtraModuleMapSearch Whether we allow to search modulemaps
644663
/// in subdirectories.
645664
///
646665
/// \returns The module named ModuleName.
647666
Module *lookupModule(StringRef ModuleName, StringRef SearchName,
667+
SourceLocation ImportLoc,
648668
bool AllowExtraModuleMapSearch = false);
649669

650670
/// Retrieve the name of the (to-be-)cached module file that should
@@ -709,6 +729,14 @@ class HeaderSearch {
709729
Module *RequestingModule,
710730
ModuleMap::KnownHeader *SuggestedModule);
711731

732+
/// Cache the result of a successful lookup at the given include location
733+
/// using the search path at index `HitIdx`.
734+
void cacheLookupSuccess(LookupFileCacheInfo &CacheLookup, unsigned HitIdx,
735+
SourceLocation IncludeLoc);
736+
/// Note that a lookup at the given include location was successful using the
737+
/// search path at index `HitIdx`.
738+
void noteLookupUsage(unsigned HitIdx, SourceLocation IncludeLoc);
739+
712740
public:
713741
/// Retrieve the module map.
714742
ModuleMap &getModuleMap() { return ModMap; }
@@ -758,6 +786,9 @@ class HeaderSearch {
758786

759787
search_dir_iterator system_dir_end() const { return SearchDirs.end(); }
760788

789+
/// Get the index of the given search directory.
790+
Optional<unsigned> searchDirIdx(const DirectoryLookup &DL) const;
791+
761792
/// Retrieve a uniqued framework name.
762793
StringRef getUniqueFrameworkName(StringRef Framework);
763794

clang/include/clang/Serialization/ASTBitCodes.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -405,6 +405,9 @@ enum UnhashedControlBlockRecordTypes {
405405

406406
/// Record code for \#pragma diagnostic mappings.
407407
DIAG_PRAGMA_MAPPINGS,
408+
409+
/// Record code for the indices of used header search entries.
410+
HEADER_SEARCH_ENTRY_USAGE,
408411
};
409412

410413
/// Record code for extension blocks.

clang/include/clang/Serialization/ModuleFile.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
#include "clang/Serialization/ASTBitCodes.h"
2121
#include "clang/Serialization/ContinuousRangeMap.h"
2222
#include "clang/Serialization/ModuleFileExtension.h"
23+
#include "llvm/ADT/BitVector.h"
2324
#include "llvm/ADT/DenseMap.h"
2425
#include "llvm/ADT/PointerIntPair.h"
2526
#include "llvm/ADT/SetVector.h"
@@ -173,6 +174,9 @@ class ModuleFile {
173174
/// unique module files based on AST contents.
174175
ASTFileSignature ASTBlockHash;
175176

177+
/// The bit vector denoting usage of each header search entry (true = used).
178+
llvm::BitVector SearchPathUsage;
179+
176180
/// Whether this module has been directly imported by the
177181
/// user.
178182
bool DirectlyImported = false;

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

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,8 @@ class DependencyScanningService {
4848
public:
4949
DependencyScanningService(ScanningMode Mode, ScanningOutputFormat Format,
5050
bool ReuseFileManager = true,
51-
bool SkipExcludedPPRanges = true);
51+
bool SkipExcludedPPRanges = true,
52+
bool OptimizeArgs = false);
5253

5354
ScanningMode getMode() const { return Mode; }
5455

@@ -58,6 +59,8 @@ class DependencyScanningService {
5859

5960
bool canSkipExcludedPPRanges() const { return SkipExcludedPPRanges; }
6061

62+
bool canOptimizeArgs() const { return OptimizeArgs; }
63+
6164
DependencyScanningFilesystemSharedCache &getSharedCache() {
6265
return SharedCache;
6366
}
@@ -70,6 +73,8 @@ class DependencyScanningService {
7073
/// ranges by bumping the buffer pointer in the lexer instead of lexing the
7174
/// tokens in the range until reaching the corresponding directive.
7275
const bool SkipExcludedPPRanges;
76+
/// Whether to optimize the modules' command-line arguments.
77+
const bool OptimizeArgs;
7378
/// The global file system cache.
7479
DependencyScanningFilesystemSharedCache SharedCache;
7580
};

clang/include/clang/Tooling/DependencyScanning/DependencyScanningWorker.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,8 @@ class DependencyScanningWorker {
8787
/// worker. If null, the file manager will not be reused.
8888
llvm::IntrusiveRefCntPtr<FileManager> Files;
8989
ScanningOutputFormat Format;
90+
/// Whether to optimize the modules' command-line arguments.
91+
bool OptimizeArgs;
9092
};
9193

9294
} // end namespace dependencies

clang/include/clang/Tooling/DependencyScanning/ModuleDepCollector.h

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,7 @@ struct ModuleDeps {
102102
bool ImportedByMainFile = false;
103103

104104
/// Compiler invocation that can be used to build this module (without paths).
105-
CompilerInvocation Invocation;
105+
CompilerInvocation BuildInvocation;
106106

107107
/// Gets the canonical command line suitable for passing to clang.
108108
///
@@ -147,8 +147,7 @@ class ModuleDepCollector;
147147
/// \c DependencyConsumer of the parent \c ModuleDepCollector.
148148
class ModuleDepCollectorPP final : public PPCallbacks {
149149
public:
150-
ModuleDepCollectorPP(CompilerInstance &I, ModuleDepCollector &MDC)
151-
: Instance(I), MDC(MDC) {}
150+
ModuleDepCollectorPP(ModuleDepCollector &MDC) : MDC(MDC) {}
152151

153152
void FileChanged(SourceLocation Loc, FileChangeReason Reason,
154153
SrcMgr::CharacteristicKind FileType,
@@ -165,8 +164,6 @@ class ModuleDepCollectorPP final : public PPCallbacks {
165164
void EndOfMainFile() override;
166165

167166
private:
168-
/// The compiler instance for the current translation unit.
169-
CompilerInstance &Instance;
170167
/// The parent dependency collector.
171168
ModuleDepCollector &MDC;
172169
/// Working set of direct modular dependencies.
@@ -199,17 +196,17 @@ class ModuleDepCollectorPP final : public PPCallbacks {
199196
class ModuleDepCollector final : public DependencyCollector {
200197
public:
201198
ModuleDepCollector(std::unique_ptr<DependencyOutputOptions> Opts,
202-
CompilerInstance &I, DependencyConsumer &C,
203-
CompilerInvocation &&OriginalCI);
199+
CompilerInstance &ScanInstance, DependencyConsumer &C,
200+
CompilerInvocation &&OriginalCI, bool OptimizeArgs);
204201

205202
void attachToPreprocessor(Preprocessor &PP) override;
206203
void attachToASTReader(ASTReader &R) override;
207204

208205
private:
209206
friend ModuleDepCollectorPP;
210207

211-
/// The compiler instance for the current translation unit.
212-
CompilerInstance &Instance;
208+
/// The compiler instance for scanning the current translation unit.
209+
CompilerInstance &ScanInstance;
213210
/// The consumer of collected dependency information.
214211
DependencyConsumer &Consumer;
215212
/// Path to the main source file.
@@ -225,15 +222,18 @@ class ModuleDepCollector final : public DependencyCollector {
225222
std::unique_ptr<DependencyOutputOptions> Opts;
226223
/// The original Clang invocation passed to dependency scanner.
227224
CompilerInvocation OriginalInvocation;
225+
/// Whether to optimize the modules' command-line arguments.
226+
bool OptimizeArgs;
228227

229228
/// Checks whether the module is known as being prebuilt.
230229
bool isPrebuiltModule(const Module *M);
231230

232231
/// Constructs a CompilerInvocation that can be used to build the given
233232
/// module, excluding paths to discovered modular dependencies that are yet to
234233
/// be built.
235-
CompilerInvocation
236-
makeInvocationForModuleBuildWithoutPaths(const ModuleDeps &Deps) const;
234+
CompilerInvocation makeInvocationForModuleBuildWithoutPaths(
235+
const ModuleDeps &Deps,
236+
llvm::function_ref<void(CompilerInvocation &)> Optimize) const;
237237
};
238238

239239
} // end namespace dependencies

0 commit comments

Comments
 (0)