Skip to content

Commit 5c6c863

Browse files
Merge branch 'main' into fix/172104-clang-cl-simd-intrinsics
2 parents 2ca9097 + 9878bac commit 5c6c863

File tree

40 files changed

+920
-288
lines changed

40 files changed

+920
-288
lines changed

clang/include/clang/DependencyScanning/DependencyScannerImpl.h

Lines changed: 5 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
#include "clang/Frontend/CompilerInvocation.h"
1818
#include "clang/Frontend/TextDiagnosticPrinter.h"
1919
#include "clang/Serialization/ObjectFilePCHContainerReader.h"
20+
#include "llvm/Support/VirtualFileSystem.h"
2021

2122
namespace clang {
2223
class DiagnosticConsumer;
@@ -143,22 +144,11 @@ class CompilerInstanceWithContext {
143144
llvm::StringRef CWD;
144145
std::vector<std::string> CommandLine;
145146

146-
// Context - file systems
147-
llvm::IntrusiveRefCntPtr<llvm::vfs::OverlayFileSystem> OverlayFS;
148-
149147
// Context - Diagnostics engine.
150-
std::unique_ptr<TextDiagnosticsPrinterWithOutput> DiagPrinterWithOS;
151-
// DiagConsumer may points to DiagPrinterWithOS->DiagPrinter, or a custom
152-
// DiagnosticConsumer passed in from initialize.
153148
DiagnosticConsumer *DiagConsumer = nullptr;
154149
std::unique_ptr<DiagnosticsEngineWithDiagOpts> DiagEngineWithCmdAndOpts;
155150

156151
// Context - compiler invocation
157-
// Compilation's command's arguments may be owned by Alloc when expanded from
158-
// response files, so we need to keep Alloc alive in the context.
159-
llvm::BumpPtrAllocator Alloc;
160-
std::unique_ptr<clang::driver::Driver> Driver;
161-
std::unique_ptr<clang::driver::Compilation> Compilation;
162152
std::unique_ptr<CompilerInvocation> OriginalInvocation;
163153

164154
// Context - output options
@@ -180,15 +170,13 @@ class CompilerInstanceWithContext {
180170
: Worker(Worker), CWD(CWD), CommandLine(CMD) {};
181171

182172
// The three methods below returns false when they fail, with the detail
183-
// accumulated in DiagConsumer.
184-
bool initialize(DiagnosticConsumer *DC);
173+
// accumulated in \c DiagEngineWithDiagOpts's diagnostic consumer.
174+
bool initialize(
175+
std::unique_ptr<DiagnosticsEngineWithDiagOpts> DiagEngineWithDiagOpts,
176+
IntrusiveRefCntPtr<llvm::vfs::OverlayFileSystem> OverlayFS);
185177
bool computeDependencies(StringRef ModuleName, DependencyConsumer &Consumer,
186178
DependencyActionController &Controller);
187179
bool finalize();
188-
189-
// The method below turns the return status from the above methods
190-
// into an llvm::Error using a default DiagnosticConsumer.
191-
llvm::Error handleReturnStatus(bool Success);
192180
};
193181
} // namespace dependencies
194182
} // namespace clang

clang/include/clang/DependencyScanning/DependencyScanningWorker.h

Lines changed: 25 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -12,12 +12,14 @@
1212
#include "clang/Basic/DiagnosticOptions.h"
1313
#include "clang/Basic/FileManager.h"
1414
#include "clang/Basic/LLVM.h"
15+
#include "clang/DependencyScanning/DependencyScannerImpl.h"
1516
#include "clang/DependencyScanning/DependencyScanningService.h"
1617
#include "clang/DependencyScanning/ModuleDepCollector.h"
1718
#include "clang/Frontend/PCHContainerOperations.h"
1819
#include "llvm/Support/Error.h"
1920
#include "llvm/Support/FileSystem.h"
2021
#include "llvm/Support/MemoryBufferRef.h"
22+
#include "llvm/Support/VirtualFileSystem.h"
2123
#include <optional>
2224
#include <string>
2325

@@ -119,42 +121,44 @@ class DependencyScanningWorker {
119121
/// dependency scanning. They together enable the dependency scanning worker
120122
/// to more effectively perform scanning for a sequence of modules
121123
/// by name when the CWD and CommandLine do not change across the queries.
124+
/// The initialization function asks the client for a DiagnosticsConsumer
125+
/// that it direct the diagnostics to.
122126

123127
/// @brief Initializing the context and the compiler instance.
124128
/// @param CWD The current working directory used during the scan.
125129
/// @param CommandLine The commandline used for the scan.
126-
/// @return Error if the initializaiton fails.
127-
llvm::Error initializeCompilerInstanceWithContextOrError(
128-
StringRef CWD, ArrayRef<std::string> CommandLine);
130+
/// @return False if the initializaiton fails.
131+
bool initializeCompilerInstanceWithContext(StringRef CWD,
132+
ArrayRef<std::string> CommandLine,
133+
DiagnosticConsumer &DC);
134+
135+
/// @brief Initializing the context and the compiler instance.
136+
/// @param CWD The current working directory used during the scan.
137+
/// @param CommandLine The commandline used for the scan.
138+
/// @param DiagEngineWithCmdAndOpts Preconfigured diagnostics engine and
139+
/// options associated with the cc1 command line.
140+
/// @param FS The overlay file system to use for this compiler instance.
141+
/// @return False if the initializaiton fails.
142+
bool initializeCompilerInstanceWithContext(
143+
StringRef CWD, ArrayRef<std::string> CommandLine,
144+
std::unique_ptr<DiagnosticsEngineWithDiagOpts> DiagEngineWithCmdAndOpts,
145+
IntrusiveRefCntPtr<llvm::vfs::OverlayFileSystem> OverlayFS);
129146

130147
/// @brief Performaces dependency scanning for the module whose name is
131148
/// specified.
132149
/// @param ModuleName The name of the module whose dependency will be
133150
/// scanned.
134151
/// @param Consumer The dependency consumer that stores the results.
135152
/// @param Controller The controller for the dependency scanning action.
136-
/// @return Error if the scanner incurs errors.
137-
llvm::Error computeDependenciesByNameWithContextOrError(
138-
StringRef ModuleName, DependencyConsumer &Consumer,
139-
DependencyActionController &Controller);
140-
141-
/// @brief Finalizes the diagnostics engine and deletes the compiler instance.
142-
/// @return Error if errors occur during finalization.
143-
llvm::Error finalizeCompilerInstanceWithContextOrError();
144-
145-
/// The three methods below provides the same functionality as the
146-
/// three methods above. Instead of returning `llvm::Error`s, these
147-
/// three methods return a flag to indicate if the call is successful.
148-
/// The initialization function asks the client for a DiagnosticsConsumer
149-
/// that it direct the diagnostics to.
150-
bool initializeCompilerInstanceWithContext(StringRef CWD,
151-
ArrayRef<std::string> CommandLine,
152-
DiagnosticConsumer *DC = nullptr);
153+
/// @return False if the scanner incurs errors.
153154
bool
154155
computeDependenciesByNameWithContext(StringRef ModuleName,
155156
DependencyConsumer &Consumer,
156157
DependencyActionController &Controller);
157-
bool finalizeCompilerInstance();
158+
159+
/// @brief Finalizes the diagnostics engine and deletes the compiler instance.
160+
/// @return False if errors occur during finalization.
161+
bool finalizeCompilerInstanceWithContext();
158162

159163
llvm::vfs::FileSystem &getVFS() const { return *DepFS; }
160164

clang/include/clang/Tooling/DependencyScanningTool.h

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
#ifndef LLVM_CLANG_TOOLING_DEPENDENCYSCANNINGTOOL_H
1010
#define LLVM_CLANG_TOOLING_DEPENDENCYSCANNINGTOOL_H
1111

12+
#include "clang/DependencyScanning/DependencyScannerImpl.h"
1213
#include "clang/DependencyScanning/DependencyScanningService.h"
1314
#include "clang/DependencyScanning/DependencyScanningUtils.h"
1415
#include "clang/DependencyScanning/DependencyScanningWorker.h"
@@ -119,9 +120,8 @@ class DependencyScanningTool {
119120
/// @param CWD The current working directory used during the scan.
120121
/// @param CommandLine The commandline used for the scan.
121122
/// @return Error if the initializaiton fails.
122-
llvm::Error
123-
initializeCompilerInstanceWithContext(StringRef CWD,
124-
ArrayRef<std::string> CommandLine);
123+
llvm::Error initializeCompilerInstanceWithContextOrError(
124+
StringRef CWD, ArrayRef<std::string> CommandLine);
125125

126126
/// @brief Computes the dependeny for the module named ModuleName.
127127
/// @param ModuleName The name of the module for which this method computes
@@ -138,7 +138,7 @@ class DependencyScanningTool {
138138
/// @return An instance of \c TranslationUnitDeps if the scan is successful.
139139
/// Otherwise it returns an error.
140140
llvm::Expected<dependencies::TranslationUnitDeps>
141-
computeDependenciesByNameWithContext(
141+
computeDependenciesByNameWithContextOrError(
142142
StringRef ModuleName,
143143
const llvm::DenseSet<dependencies::ModuleID> &AlreadySeen,
144144
dependencies::LookupModuleOutputCallback LookupModuleOutput);
@@ -147,7 +147,7 @@ class DependencyScanningTool {
147147
/// diagnostics and deletes the compiler instance. Call this method
148148
/// once all names for a same commandline are scanned.
149149
/// @return Error if an error occured during finalization.
150-
llvm::Error finalizeCompilerInstanceWithContext();
150+
llvm::Error finalizeCompilerInstanceWithContextOrError();
151151

152152
llvm::vfs::FileSystem &getWorkerVFS() const { return Worker.getVFS(); }
153153

clang/lib/DependencyScanning/DependencyScannerImpl.cpp

Lines changed: 18 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -713,38 +713,25 @@ bool DependencyScanningAction::runInvocation(
713713
return Result;
714714
}
715715

716-
bool CompilerInstanceWithContext::initialize(DiagnosticConsumer *DC) {
717-
if (DC) {
718-
DiagConsumer = DC;
719-
} else {
720-
DiagPrinterWithOS =
721-
std::make_unique<TextDiagnosticsPrinterWithOutput>(CommandLine);
722-
DiagConsumer = &DiagPrinterWithOS->DiagPrinter;
723-
}
724-
725-
std::tie(OverlayFS, CommandLine) = initVFSForByNameScanning(
726-
Worker.DepFS, CommandLine, CWD, "ScanningByName");
727-
728-
DiagEngineWithCmdAndOpts = std::make_unique<DiagnosticsEngineWithDiagOpts>(
729-
CommandLine, OverlayFS, *DiagConsumer);
730-
731-
std::tie(Driver, Compilation) = buildCompilation(
732-
CommandLine, *DiagEngineWithCmdAndOpts->DiagEngine, OverlayFS, Alloc);
733-
734-
if (!Compilation)
735-
return false;
716+
bool CompilerInstanceWithContext::initialize(
717+
std::unique_ptr<DiagnosticsEngineWithDiagOpts> DiagEngineWithDiagOpts,
718+
IntrusiveRefCntPtr<llvm::vfs::OverlayFileSystem> OverlayFS) {
719+
assert(DiagEngineWithDiagOpts && "Valid diagnostics engine required!");
720+
DiagEngineWithCmdAndOpts = std::move(DiagEngineWithDiagOpts);
721+
DiagConsumer = DiagEngineWithCmdAndOpts->DiagEngine->getClient();
722+
723+
#ifndef NDEBUG
724+
assert(OverlayFS && "OverlayFS required!");
725+
bool SawDepFS = false;
726+
OverlayFS->visit([&](llvm::vfs::FileSystem &VFS) {
727+
SawDepFS |= &VFS == Worker.DepFS.get();
728+
});
729+
assert(SawDepFS && "OverlayFS not based on DepFS");
730+
#endif
736731

737-
assert(Compilation->getJobs().size() &&
738-
"Must have a job list of non-zero size");
739-
const driver::Command &Command = *(Compilation->getJobs().begin());
740-
const auto &CommandArgs = Command.getArguments();
741-
assert(!CommandArgs.empty() && "Cannot have a command with 0 args");
742-
assert(StringRef(CommandArgs[0]) == "-cc1" && "Requires a cc1 job.");
743-
OriginalInvocation = std::make_unique<CompilerInvocation>();
744-
745-
if (!CompilerInvocation::CreateFromArgs(*OriginalInvocation, CommandArgs,
746-
*DiagEngineWithCmdAndOpts->DiagEngine,
747-
Command.getExecutable())) {
732+
OriginalInvocation = createCompilerInvocation(
733+
CommandLine, *DiagEngineWithCmdAndOpts->DiagEngine);
734+
if (!OriginalInvocation) {
748735
DiagEngineWithCmdAndOpts->DiagEngine->Report(
749736
diag::err_fe_expected_compiler_job)
750737
<< llvm::join(CommandLine, " ");
@@ -876,11 +863,3 @@ bool CompilerInstanceWithContext::finalize() {
876863
DiagConsumer->finish();
877864
return true;
878865
}
879-
880-
llvm::Error CompilerInstanceWithContext::handleReturnStatus(bool Success) {
881-
assert(DiagPrinterWithOS && "Must use the default DiagnosticConsumer.");
882-
return Success ? llvm::Error::success()
883-
: llvm::make_error<llvm::StringError>(
884-
DiagPrinterWithOS->DiagnosticsOS.str(),
885-
llvm::inconvertibleErrorCode());
886-
}

clang/lib/DependencyScanning/DependencyScanningWorker.cpp

Lines changed: 22 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,13 @@
77
//===----------------------------------------------------------------------===//
88

99
#include "clang/DependencyScanning/DependencyScanningWorker.h"
10+
#include "clang/Basic/Diagnostic.h"
1011
#include "clang/Basic/DiagnosticFrontend.h"
1112
#include "clang/DependencyScanning/DependencyScannerImpl.h"
1213
#include "clang/Driver/Driver.h"
1314
#include "clang/Driver/Tool.h"
15+
#include "clang/Serialization/ObjectFilePCHContainerReader.h"
16+
#include "llvm/Support/VirtualFileSystem.h"
1417

1518
using namespace clang;
1619
using namespace dependencies;
@@ -165,33 +168,29 @@ bool DependencyScanningWorker::computeDependencies(
165168
DC);
166169
}
167170

168-
llvm::Error
169-
DependencyScanningWorker::initializeCompilerInstanceWithContextOrError(
170-
StringRef CWD, ArrayRef<std::string> CommandLine) {
171-
bool Success = initializeCompilerInstanceWithContext(CWD, CommandLine);
172-
return CIWithContext->handleReturnStatus(Success);
173-
}
174-
175-
llvm::Error
176-
DependencyScanningWorker::computeDependenciesByNameWithContextOrError(
177-
StringRef ModuleName, DependencyConsumer &Consumer,
178-
DependencyActionController &Controller) {
179-
bool Success =
180-
computeDependenciesByNameWithContext(ModuleName, Consumer, Controller);
181-
return CIWithContext->handleReturnStatus(Success);
182-
}
183-
184-
llvm::Error
185-
DependencyScanningWorker::finalizeCompilerInstanceWithContextOrError() {
186-
bool Success = finalizeCompilerInstance();
187-
return CIWithContext->handleReturnStatus(Success);
171+
bool DependencyScanningWorker::initializeCompilerInstanceWithContext(
172+
StringRef CWD, ArrayRef<std::string> CommandLine, DiagnosticConsumer &DC) {
173+
auto OverlayFSAndArgs =
174+
initVFSForByNameScanning(DepFS, CommandLine, CWD, "ScanningByName");
175+
auto &OverlayFS = OverlayFSAndArgs.first;
176+
const auto &ModifiedCommandLine = OverlayFSAndArgs.second;
177+
178+
auto DiagEngineWithCmdAndOpts =
179+
std::make_unique<DiagnosticsEngineWithDiagOpts>(ModifiedCommandLine,
180+
OverlayFS, DC);
181+
182+
return initializeCompilerInstanceWithContext(
183+
CWD, ModifiedCommandLine, std::move(DiagEngineWithCmdAndOpts), OverlayFS);
188184
}
189185

190186
bool DependencyScanningWorker::initializeCompilerInstanceWithContext(
191-
StringRef CWD, ArrayRef<std::string> CommandLine, DiagnosticConsumer *DC) {
187+
StringRef CWD, ArrayRef<std::string> CommandLine,
188+
std::unique_ptr<DiagnosticsEngineWithDiagOpts> DiagEngineWithDiagOpts,
189+
IntrusiveRefCntPtr<llvm::vfs::OverlayFileSystem> OverlayFS) {
192190
CIWithContext =
193191
std::make_unique<CompilerInstanceWithContext>(*this, CWD, CommandLine);
194-
return CIWithContext->initialize(DC);
192+
return CIWithContext->initialize(std::move(DiagEngineWithDiagOpts),
193+
OverlayFS);
195194
}
196195

197196
bool DependencyScanningWorker::computeDependenciesByNameWithContext(
@@ -201,6 +200,6 @@ bool DependencyScanningWorker::computeDependenciesByNameWithContext(
201200
return CIWithContext->computeDependencies(ModuleName, Consumer, Controller);
202201
}
203202

204-
bool DependencyScanningWorker::finalizeCompilerInstance() {
203+
bool DependencyScanningWorker::finalizeCompilerInstanceWithContext() {
205204
return CIWithContext->finalize();
206205
}

clang/lib/StaticAnalyzer/Checkers/WebKit/PtrTypesSemantics.cpp

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -523,6 +523,15 @@ class TrivialFunctionAnalysisVisitor
523523
});
524524
}
525525

526+
bool IsStatementTrivial(const Stmt *S) {
527+
auto CacheIt = Cache.find(S);
528+
if (CacheIt != Cache.end())
529+
return CacheIt->second;
530+
bool Result = Visit(S);
531+
Cache[S] = Result;
532+
return Result;
533+
}
534+
526535
bool VisitStmt(const Stmt *S) {
527536
// All statements are non-trivial unless overriden later.
528537
// Don't even recurse into children by default.
@@ -826,9 +835,7 @@ bool TrivialFunctionAnalysis::isTrivialImpl(
826835
bool TrivialFunctionAnalysis::isTrivialImpl(
827836
const Stmt *S, TrivialFunctionAnalysis::CacheTy &Cache) {
828837
TrivialFunctionAnalysisVisitor V(Cache);
829-
bool Result = V.Visit(S);
830-
assert(Cache.contains(S) && "Top-level statement not properly cached!");
831-
return Result;
838+
return V.IsStatementTrivial(S);
832839
}
833840

834841
} // namespace clang

clang/lib/StaticAnalyzer/Checkers/WebKit/RawPtrRefLambdaCapturesChecker.cpp

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -343,6 +343,17 @@ class RawPtrRefLambdaCapturesChecker
343343
auto *Callee = CE->getCallee();
344344
if (!Callee)
345345
return;
346+
Callee = Callee->IgnoreParenCasts();
347+
if (auto *MTE = dyn_cast<MaterializeTemporaryExpr>(Callee)) {
348+
Callee = MTE->getSubExpr();
349+
if (!Callee)
350+
return;
351+
Callee = Callee->IgnoreParenCasts();
352+
}
353+
if (auto *L = dyn_cast<LambdaExpr>(Callee)) {
354+
LambdasToIgnore.insert(L); // Calling a lambda upon creation is safe.
355+
return;
356+
}
346357
auto *DRE = dyn_cast<DeclRefExpr>(Callee->IgnoreParenCasts());
347358
if (!DRE)
348359
return;

clang/lib/StaticAnalyzer/Checkers/WebKit/RawPtrRefLocalVarsChecker.cpp

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -234,6 +234,14 @@ class RawPtrRefLocalVarsChecker
234234
}
235235

236236
bool TraverseIfStmt(IfStmt *IS) override {
237+
if (IS->getConditionVariable()) {
238+
// This code currently does not explicitly check the "else" statement
239+
// since getConditionVariable returns nullptr when there is a
240+
// condition defined after ";" as in "if (auto foo = ~; !foo)". If
241+
// this semantics change, we should add an explicit check for "else".
242+
if (auto *Then = IS->getThen(); !Then || TFA.isTrivial(Then))
243+
return true;
244+
}
237245
if (!TFA.isTrivial(IS))
238246
return DynamicRecursiveASTVisitor::TraverseIfStmt(IS);
239247
return true;

0 commit comments

Comments
 (0)