-
Notifications
You must be signed in to change notification settings - Fork 15.2k
[Modules] Record whether VarDecl initializers contain side effects #143739
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -1442,6 +1442,10 @@ class ASTReader | |
const StringRef &operator*() && = delete; | ||
}; | ||
|
||
/// VarDecls with initializers containing side effects must be emitted, | ||
/// but DeclMustBeEmitted is not allowed to deserialize the intializer. | ||
llvm::SmallPtrSet<Decl *, 16> InitSideEffectVars; | ||
|
||
|
||
public: | ||
/// Get the buffer for resolving paths. | ||
SmallString<0> &getPathBuf() { return PathBuf; } | ||
|
@@ -2392,6 +2396,8 @@ class ASTReader | |
|
||
bool wasThisDeclarationADefinition(const FunctionDecl *FD) override; | ||
|
||
bool hasInitializerWithSideEffects(const VarDecl *VD) const override; | ||
|
||
/// Retrieve a selector from the given module with its local ID | ||
/// number. | ||
Selector getLocalSelector(ModuleFile &M, unsigned LocalID); | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2434,6 +2434,31 @@ VarDecl *VarDecl::getInitializingDeclaration() { | |
return Def; | ||
} | ||
|
||
bool VarDecl::hasInitWithSideEffects() const { | ||
if (!hasInit()) | ||
return false; | ||
|
||
// Check if we can get the initializer without deserializing | ||
const Expr *E = nullptr; | ||
if (auto *S = dyn_cast<Stmt *>(Init)) { | ||
hnrklssn marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
E = cast<Expr>(S); | ||
} else { | ||
auto *Eval = getEvaluatedStmt(); | ||
if (!Eval->Value.isOffset()) | ||
E = cast<Expr>(Eval->Value.get(nullptr)); | ||
hnrklssn marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
} | ||
|
||
if (E) | ||
return E->HasSideEffects(getASTContext()) && | ||
// We can get a value-dependent initializer during error recovery. | ||
(E->isValueDependent() || !evaluateValue()); | ||
|
||
assert(getEvaluatedStmt()->Value.isOffset()); | ||
// ASTReader tracks this without having to deserialize the initializer | ||
return getASTContext().getExternalSource()->hasInitializerWithSideEffects( | ||
|
||
this); | ||
} | ||
|
||
bool VarDecl::isOutOfLine() const { | ||
if (Decl::isOutOfLine()) | ||
return true; | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
// Tests referencing variable with initializer containing side effect across module boundary | ||
// | ||
// RUN: rm -rf %t | ||
// RUN: mkdir -p %t | ||
// RUN: split-file %s %t | ||
|
||
// RUN: %clang_cc1 -std=c++20 -emit-module-interface %t/Foo.cppm \ | ||
// RUN: -o %t/Foo.pcm | ||
|
||
// RUN: %clang_cc1 -std=c++20 -emit-module-interface \ | ||
// RUN: -fmodule-file=Foo=%t/Foo.pcm \ | ||
// RUN: %t/Bar.cppm \ | ||
// RUN: -o %t/Bar.pcm | ||
|
||
// RUN: %clang_cc1 -std=c++20 -emit-obj \ | ||
// RUN: -main-file-name Bar.cppm \ | ||
// RUN: -fmodule-file=Foo=%t/Foo.pcm \ | ||
// RUN: -x pcm %t/Bar.pcm \ | ||
// RUN: -o %t/Bar.o | ||
|
||
//--- Foo.cppm | ||
export module Foo; | ||
|
||
export { | ||
class S {}; | ||
|
||
inline S s = S{}; | ||
} | ||
|
||
//--- Bar.cppm | ||
export module Bar; | ||
import Foo; | ||
|
||
S bar() { | ||
return s; | ||
} | ||
|
Uh oh!
There was an error while loading. Please reload this page.