Skip to content
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

Don't set init and init style of a declaration manually #1232

Merged
merged 2 commits into from
Feb 7, 2025

Conversation

PetroZarytskyi
Copy link
Collaborator

This PR addresses 2 issues:

  1. Initialization styles should not be set manually, instead, they should be deduced by clang based on the types of the declaration and init, as well as on whether the declaration is direct.
    e.g. direct initialization of std::vector vec with other should look like
std::vector vec(other);

and not

std::vector vec = other;
  1. Sometimes we initialize declarations using Decl::setInit, which is a low-level function used by clang internally. This PR introduces a wrapper of Sema::AddInitializerToDecl called SetDeclInit.

Copy link
Contributor

@github-actions github-actions bot left a comment

Choose a reason for hiding this comment

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

clang-tidy made some suggestions

include/clad/Differentiator/VisitorBase.h Show resolved Hide resolved
include/clad/Differentiator/VisitorBase.h Show resolved Hide resolved
lib/Differentiator/HessianModeVisitor.cpp Outdated Show resolved Hide resolved
lib/Differentiator/VectorForwardModeVisitor.cpp Outdated Show resolved Hide resolved

// Clang expects direct inits to be wrapped either in InitListExpr or
// ParenListExpr.
if (DirectInit && !isa<InitListExpr>(Init) && !isa<ParenListExpr>(Init))
Copy link
Contributor

Choose a reason for hiding this comment

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

warning: no header providing "clang::isa" is directly included [misc-include-cleaner]

    if (DirectInit && !isa<InitListExpr>(Init) && !isa<ParenListExpr>(Init))
                       ^

Copy link

codecov bot commented Feb 3, 2025

Codecov Report

Attention: Patch coverage is 93.75000% with 3 lines in your changes missing coverage. Please review.

Project coverage is 94.66%. Comparing base (220064e) to head (cf8540b).
Report is 2 commits behind head on master.

Files with missing lines Patch % Lines
lib/Differentiator/ReverseModeVisitor.cpp 84.21% 3 Missing ⚠️
Additional details and impacted files

Impacted file tree graph

@@            Coverage Diff             @@
##           master    #1232      +/-   ##
==========================================
+ Coverage   94.64%   94.66%   +0.02%     
==========================================
  Files          51       51              
  Lines        8890     8886       -4     
==========================================
- Hits         8414     8412       -2     
+ Misses        476      474       -2     
Files with missing lines Coverage Δ
include/clad/Differentiator/ReverseModeVisitor.h 98.09% <100.00%> (ø)
include/clad/Differentiator/VisitorBase.h 100.00% <ø> (ø)
lib/Differentiator/BaseForwardModeVisitor.cpp 98.70% <100.00%> (ø)
lib/Differentiator/HessianModeVisitor.cpp 99.53% <100.00%> (ø)
lib/Differentiator/JacobianModeVisitor.cpp 93.93% <100.00%> (-0.04%) ⬇️
lib/Differentiator/VectorForwardModeVisitor.cpp 99.69% <100.00%> (ø)
lib/Differentiator/VisitorBase.cpp 97.76% <100.00%> (+0.02%) ⬆️
lib/Differentiator/ReverseModeVisitor.cpp 95.68% <84.21%> (+0.06%) ⬆️
Files with missing lines Coverage Δ
include/clad/Differentiator/ReverseModeVisitor.h 98.09% <100.00%> (ø)
include/clad/Differentiator/VisitorBase.h 100.00% <ø> (ø)
lib/Differentiator/BaseForwardModeVisitor.cpp 98.70% <100.00%> (ø)
lib/Differentiator/HessianModeVisitor.cpp 99.53% <100.00%> (ø)
lib/Differentiator/JacobianModeVisitor.cpp 93.93% <100.00%> (-0.04%) ⬇️
lib/Differentiator/VectorForwardModeVisitor.cpp 99.69% <100.00%> (ø)
lib/Differentiator/VisitorBase.cpp 97.76% <100.00%> (+0.02%) ⬆️
lib/Differentiator/ReverseModeVisitor.cpp 95.68% <84.21%> (+0.06%) ⬆️

@PetroZarytskyi PetroZarytskyi force-pushed the init-style branch 3 times, most recently from 837b88d to 3b6762b Compare February 3, 2025 13:48
Copy link
Contributor

@github-actions github-actions bot left a comment

Choose a reason for hiding this comment

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

clang-tidy made some suggestions

Copy link
Owner

@vgvassilev vgvassilev left a comment

Choose a reason for hiding this comment

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

This looks pretty awesome. See comments inline.

include/clad/Differentiator/VisitorBase.h Outdated Show resolved Hide resolved
lib/Differentiator/ReverseModeVisitor.cpp Show resolved Hide resolved
// not reset the init and we have to do it manually.
VD->setInit(nullptr);
m_Sema.ActOnUninitializedDecl(VD);
return;
Copy link
Owner

Choose a reason for hiding this comment

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

Does ActOnUninitializedDecl set the InitStyle, too? If so we should update the comment.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Does init style matter when it comes to uninitialized decls?

Copy link
Owner

Choose a reason for hiding this comment

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

Depends on what clang does... Probably does not but then we need to update the documentation comment of SetDeclInit.

@PetroZarytskyi PetroZarytskyi force-pushed the init-style branch 2 times, most recently from 4d8732f to 4dabcd7 Compare February 3, 2025 23:09
@@ -16,6 +16,8 @@
#include "clang/Sema/ParsedAttr.h"
#include "clang/Sema/Sema.h"
#include <array>
#include <clang/AST/Type.h>
#include <llvm/ADT/StringRef.h>
Copy link
Owner

Choose a reason for hiding this comment

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

We should put these before array and sort alphabetically.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

I tried ordering everything alphabetically but clang-format reverted all changes. I separated the includes into different blocks, clang-format seems to be okay with this. I hope this is better.

include/clad/Differentiator/VisitorBase.h Outdated Show resolved Hide resolved
@@ -5,6 +5,7 @@

#include "clang/AST/TemplateName.h"
#include "clang/Sema/Lookup.h"
#include <clang/AST/Decl.h>
Copy link
Owner

Choose a reason for hiding this comment

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

That needs to be sorted..

@PetroZarytskyi PetroZarytskyi force-pushed the init-style branch 2 times, most recently from 4f17027 to 9415eca Compare February 7, 2025 15:27
Initialization styles should not be set manually, instead, they should be deduced by clang based on the types of the declaration and init, as well as on whether the declaration is direct.
 e.g. direct initialization of `std::vector vec` with `other` should look like
```
std::vector vec(other);
```
and not
```
std::vector vec = other;
```
Sometimes we initialize declarations using ``Decl::setInit``, which is a low-level function used by clang internally. This commit introduces a wrapper of ``Sema::AddInitializerToDecl`` called ``SetDeclInit``.
Copy link
Owner

@vgvassilev vgvassilev left a comment

Choose a reason for hiding this comment

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

LGTM!

@vgvassilev vgvassilev merged commit e6d200d into vgvassilev:master Feb 7, 2025
97 of 98 checks passed
@PetroZarytskyi PetroZarytskyi deleted the init-style branch February 10, 2025 13:12
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants