Skip to content

[Clang][NFC] Simplify initialization of OverloadCandidate objects. #100318

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

Merged
merged 1 commit into from
Jul 24, 2024

Conversation

cor3ntin
Copy link
Contributor

Initialize some fields of OverloadCandidate in its constructor. The goal here is try to fix read of uninitialized variable (which I was not able to reproduce)
#93430 (comment)

We should certainly try to improve the construction of OverloadCandidate further as it can be quite britle.

@llvmbot llvmbot added clang Clang issues not falling into any other category clang:frontend Language frontend issues, e.g. anything involving "Sema" labels Jul 24, 2024
@cor3ntin cor3ntin requested a review from dwblaikie July 24, 2024 08:02
@llvmbot
Copy link
Member

llvmbot commented Jul 24, 2024

@llvm/pr-subscribers-clang

Author: cor3ntin (cor3ntin)

Changes

Initialize some fields of OverloadCandidate in its constructor. The goal here is try to fix read of uninitialized variable (which I was not able to reproduce)
#93430 (comment)

We should certainly try to improve the construction of OverloadCandidate further as it can be quite britle.


Full diff: https://github.com/llvm/llvm-project/pull/100318.diff

2 Files Affected:

  • (modified) clang/include/clang/Sema/Overload.h (+2-1)
  • (modified) clang/lib/Sema/SemaOverload.cpp (-19)
diff --git a/clang/include/clang/Sema/Overload.h b/clang/include/clang/Sema/Overload.h
index 9d8b797af6663..2cf30d7b11709 100644
--- a/clang/include/clang/Sema/Overload.h
+++ b/clang/include/clang/Sema/Overload.h
@@ -998,7 +998,8 @@ class Sema;
   private:
     friend class OverloadCandidateSet;
     OverloadCandidate()
-        : IsSurrogate(false), IsADLCandidate(CallExpr::NotADL), RewriteKind(CRK_None) {}
+        : IsSurrogate(false), IgnoreObjectArgument(false), TookAddressOfOverload(false),
+          IsADLCandidate(CallExpr::NotADL), RewriteKind(CRK_None) {}
   };
 
   /// OverloadCandidateSet - A set of overload candidates, used in C++
diff --git a/clang/lib/Sema/SemaOverload.cpp b/clang/lib/Sema/SemaOverload.cpp
index a8d250fbabfed..7c2167c23bcd8 100644
--- a/clang/lib/Sema/SemaOverload.cpp
+++ b/clang/lib/Sema/SemaOverload.cpp
@@ -6857,10 +6857,7 @@ void Sema::AddOverloadCandidate(
   Candidate.Viable = true;
   Candidate.RewriteKind =
       CandidateSet.getRewriteInfo().getRewriteKind(Function, PO);
-  Candidate.IsSurrogate = false;
   Candidate.IsADLCandidate = IsADLCandidate;
-  Candidate.IgnoreObjectArgument = false;
-  Candidate.TookAddressOfOverload = false;
   Candidate.ExplicitCallArguments = Args.size();
 
   // Explicit functions are not actually candidates at all if we're not
@@ -7422,8 +7419,6 @@ Sema::AddMethodCandidate(CXXMethodDecl *Method, DeclAccessPair FoundDecl,
   Candidate.Function = Method;
   Candidate.RewriteKind =
       CandidateSet.getRewriteInfo().getRewriteKind(Method, PO);
-  Candidate.IsSurrogate = false;
-  Candidate.IgnoreObjectArgument = false;
   Candidate.TookAddressOfOverload =
       CandidateSet.getKind() == OverloadCandidateSet::CSK_AddressOfOverloadSet;
   Candidate.ExplicitCallArguments = Args.size();
@@ -7617,7 +7612,6 @@ void Sema::AddMethodTemplateCandidate(
     Candidate.IgnoreObjectArgument =
         cast<CXXMethodDecl>(Candidate.Function)->isStatic() ||
         ObjectType.isNull();
-    Candidate.TookAddressOfOverload = false;
     Candidate.ExplicitCallArguments = Args.size();
     if (Result == TemplateDeductionResult::NonDependentConversionFailure)
       Candidate.FailureKind = ovl_fail_bad_conversion;
@@ -7705,7 +7699,6 @@ void Sema::AddTemplateOverloadCandidate(
     Candidate.IgnoreObjectArgument =
         isa<CXXMethodDecl>(Candidate.Function) &&
         !isa<CXXConstructorDecl>(Candidate.Function);
-    Candidate.TookAddressOfOverload = false;
     Candidate.ExplicitCallArguments = Args.size();
     if (Result == TemplateDeductionResult::NonDependentConversionFailure)
       Candidate.FailureKind = ovl_fail_bad_conversion;
@@ -7886,9 +7879,6 @@ void Sema::AddConversionCandidate(
   OverloadCandidate &Candidate = CandidateSet.addCandidate(1);
   Candidate.FoundDecl = FoundDecl;
   Candidate.Function = Conversion;
-  Candidate.IsSurrogate = false;
-  Candidate.IgnoreObjectArgument = false;
-  Candidate.TookAddressOfOverload = false;
   Candidate.FinalConversion.setAsIdentityConversion();
   Candidate.FinalConversion.setFromType(ConvType);
   Candidate.FinalConversion.setAllToTypes(ToType);
@@ -8084,9 +8074,6 @@ void Sema::AddTemplateConversionCandidate(
     Candidate.Function = FunctionTemplate->getTemplatedDecl();
     Candidate.Viable = false;
     Candidate.FailureKind = ovl_fail_bad_deduction;
-    Candidate.IsSurrogate = false;
-    Candidate.IgnoreObjectArgument = false;
-    Candidate.TookAddressOfOverload = false;
     Candidate.ExplicitCallArguments = 1;
     Candidate.DeductionFailure = MakeDeductionFailureInfo(Context, Result,
                                                           Info);
@@ -8120,9 +8107,6 @@ void Sema::AddSurrogateCandidate(CXXConversionDecl *Conversion,
   Candidate.Function = nullptr;
   Candidate.Surrogate = Conversion;
   Candidate.Viable = true;
-  Candidate.IsSurrogate = true;
-  Candidate.IgnoreObjectArgument = false;
-  Candidate.TookAddressOfOverload = false;
   Candidate.ExplicitCallArguments = Args.size();
 
   // Determine the implicit conversion sequence for the implicit
@@ -8328,9 +8312,6 @@ void Sema::AddBuiltinCandidate(QualType *ParamTys, ArrayRef<Expr *> Args,
   OverloadCandidate &Candidate = CandidateSet.addCandidate(Args.size());
   Candidate.FoundDecl = DeclAccessPair::make(nullptr, AS_none);
   Candidate.Function = nullptr;
-  Candidate.IsSurrogate = false;
-  Candidate.IgnoreObjectArgument = false;
-  Candidate.TookAddressOfOverload = false;
   std::copy(ParamTys, ParamTys + Args.size(), Candidate.BuiltinParamTypes);
 
   // Determine the implicit conversion sequences for each of the

Copy link

github-actions bot commented Jul 24, 2024

✅ With the latest revision this PR passed the C/C++ code formatter.

@cor3ntin cor3ntin force-pushed the corentin/fix93430l-2 branch from 9edd7f7 to b0839b4 Compare July 24, 2024 08:05
Initialize some fields of OverloadCandidate in its constructor.
The goal here is try to fix  read of uninitialized variable
(which I was not able to reproduce)
llvm#93430 (comment)

We should certainly try to improve the construction of
`OverloadCandidate` further as it can be quite britle.
@cor3ntin cor3ntin force-pushed the corentin/fix93430l-2 branch from b0839b4 to c61324e Compare July 24, 2024 09:07
Copy link
Member

@Sirraide Sirraide left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Moving more stuff into the constructor makes sense.

I’m not too familiar w/ the code around overloading, but would it be possible to actually write a proper constructor for it at this point (which would take e.g. the Function and other things as an argument) to ensure that every field is initialised, instead of just manually initialising a lot of the fields afterwards which we seem to be doing atm? Or is there a reason why we can’t do that here?

@cor3ntin
Copy link
Contributor Author

I’m not too familiar w/ the code around overloading, but would it be possible to actually write a proper constructor for it at this point (which would take e.g. the Function and other things as an argument) to ensure that every field is initialised, instead of just manually initialising a lot of the fields afterwards which we seem to be doing atm? Or is there a reason why we can’t do that here?

We might be able to, and frankly we should but it's a slightly bigger refactor that I don't really have time to investigate
(right now addCandidate is what constructs OverloadCandidate objects, a I'm not sure adding a bunch of parameters to that would be a good thing)

@Sirraide
Copy link
Member

We might be able to, and frankly we should but it's a slightly bigger refactor that I don't really have time to investigate

Makes sense.

@cor3ntin cor3ntin merged commit 7d787df into llvm:main Jul 24, 2024
7 checks passed
@cor3ntin cor3ntin added this to the LLVM 19.X Release milestone Jul 24, 2024
@cor3ntin
Copy link
Contributor Author

/cherry-pick 7d787df

llvmbot pushed a commit to llvmbot/llvm-project that referenced this pull request Jul 24, 2024
…lvm#100318)

Initialize some fields of OverloadCandidate in its constructor. The goal
here is try to fix read of uninitialized variable (which I was not able
to reproduce)
llvm#93430 (comment)

We should certainly try to improve the construction of
`OverloadCandidate` further as it can be quite britle.

(cherry picked from commit 7d787df)
@llvmbot
Copy link
Member

llvmbot commented Jul 24, 2024

/pull-request #100410

yuxuanchen1997 pushed a commit that referenced this pull request Jul 25, 2024
…100318)

Summary:
Initialize some fields of OverloadCandidate in its constructor. The goal
here is try to fix read of uninitialized variable (which I was not able
to reproduce)
#93430 (comment)

We should certainly try to improve the construction of
`OverloadCandidate` further as it can be quite britle.

Test Plan: 

Reviewers: 

Subscribers: 

Tasks: 

Tags: 


Differential Revision: https://phabricator.intern.facebook.com/D60250652
tru pushed a commit to llvmbot/llvm-project that referenced this pull request Jul 26, 2024
…lvm#100318)

Initialize some fields of OverloadCandidate in its constructor. The goal
here is try to fix read of uninitialized variable (which I was not able
to reproduce)
llvm#93430 (comment)

We should certainly try to improve the construction of
`OverloadCandidate` further as it can be quite britle.

(cherry picked from commit 7d787df)
@cor3ntin cor3ntin self-assigned this Aug 5, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
clang:frontend Language frontend issues, e.g. anything involving "Sema" clang Clang issues not falling into any other category
Projects
Development

Successfully merging this pull request may close these issues.

3 participants