Skip to content

Add DiagnosticsEngineRAII#849

Open
codegallivant wants to merge 1 commit intocompiler-research:mainfrom
codegallivant:diags-raii
Open

Add DiagnosticsEngineRAII#849
codegallivant wants to merge 1 commit intocompiler-research:mainfrom
codegallivant:diags-raii

Conversation

@codegallivant
Copy link
Copy Markdown
Contributor

Description

Fixes FIXME: this can go into a RAII object. Reference:

// FIXME: this can go into a RAII object

Implementation details-
I added a RAII class (compat::DiagnosticsEngineRAII) that automatically checks if it should reset the DiagnosticsEngine before the RAII object goes out of scope. It checks for diags.hasErrorOccurred() and a user-provided boolean reset_condition.

Type of change

Please tick all options which are relevant.

  • Bug fix
  • New feature
  • Requires documentation updates

Testing

100% tests passed, 0 tests failed out of 3

Label Time Summary:
DEPENDS    =  12.35 sec*proc (3 tests)

Total Test time (real) =  12.36 sec
[100%] Built target check-cppinterop

Checklist

  • I have read the contribution guide recently

Copy link
Copy Markdown
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

Comment thread lib/CppInterOp/Compatibility.h Outdated
// ~SynthesizingCodeRAII() {} // TODO: implement
};

class DiagnosticsEngineRAII {
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

warning: class 'DiagnosticsEngineRAII' defines a non-default destructor but does not define a copy constructor, a copy assignment operator, a move constructor or a move assignment operator [cppcoreguidelines-special-member-functions]

class DiagnosticsEngineRAII {
      ^

Comment thread lib/CppInterOp/Compatibility.h Outdated

class DiagnosticsEngineRAII {
private:
clang::DiagnosticsEngine& diags;
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

warning: member 'diags' of type 'clang::DiagnosticsEngine &' is a reference [cppcoreguidelines-avoid-const-or-ref-data-members]

  clang::DiagnosticsEngine& diags;
                            ^

Comment thread lib/CppInterOp/Compatibility.h Outdated
clang::DiagnosticsEngine& diags;

public:
bool reset_condition; // additional condition to reset diagnostics
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

warning: member variable 'reset_condition' has public visibility [cppcoreguidelines-non-private-member-variables-in-classes]

  bool reset_condition; // additional condition to reset diagnostics
       ^

@codecov
Copy link
Copy Markdown

codecov Bot commented Mar 10, 2026

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 79.74%. Comparing base (4e3e4c2) to head (fec15ed).

Additional details and impacted files

Impacted file tree graph

@@            Coverage Diff             @@
##             main     #849      +/-   ##
==========================================
+ Coverage   79.72%   79.74%   +0.02%     
==========================================
  Files          11       11              
  Lines        4025     4029       +4     
==========================================
+ Hits         3209     3213       +4     
  Misses        816      816              
Files with missing lines Coverage Δ
lib/CppInterOp/Compatibility.h 87.60% <100.00%> (+0.64%) ⬆️
lib/CppInterOp/CppInterOp.cpp 87.94% <100.00%> (-0.02%) ⬇️
Files with missing lines Coverage Δ
lib/CppInterOp/Compatibility.h 87.60% <100.00%> (+0.64%) ⬆️
lib/CppInterOp/CppInterOp.cpp 87.94% <100.00%> (-0.02%) ⬇️
🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

Copy link
Copy Markdown
Collaborator

@Vipul-Cariappa Vipul-Cariappa left a comment

Choose a reason for hiding this comment

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

Can you check if clang's SFINAETrap (Ref: https://clang.llvm.org/doxygen/classclang_1_1Sema_1_1SFINAETrap.html#details) does the necessary trick? i.e. can we replace our custom logic with this object instead?

@codegallivant
Copy link
Copy Markdown
Contributor Author

codegallivant commented Mar 11, 2026

Can you check if clang's SFINAETrap (Ref: https://clang.llvm.org/doxygen/classclang_1_1Sema_1_1SFINAETrap.html#details) does the necessary trick? i.e. can we replace our custom logic with this object instead?

@Vipul-Cariappa I spent some time reading up on a few things. From what I've found, this is my understanding:

  1. SFINAETrap() only suppresses errors that are not outside the immediate context of substitution i.e. not outside the function's signature. SFINAETrap() is supposed to be used to parse template/overloaded function signatures without causing false errors. It is internally hard-coded to not suppress errors resulting from the function's body itself.
  2. Refer to the following snippet from CppImpl::InstantiateFunctionDefinition -
    getSema().InstantiateFunctionDefinition(SourceLocation(), FD,
                                                /*Recursive=*/true,
                                                /*DefinitionRequired=*/true // look at this argument
    and I found this in sema.h-
      /// \param DefinitionRequired if true, then we are performing an explicit
      /// instantiation where the body of the function is required. Complain if
      /// there is no such body.
    The DefinitionRequired=true argument asks clang to mandatorily parse the function's body and add it to the AST. So, an error that results from this will be a hard error and would not get suppressed by SFINAETrap(). Therefore, I do not think using SFINAETrap() here would be useful.

Sources:

@codegallivant
Copy link
Copy Markdown
Contributor Author

codegallivant commented Mar 12, 2026

cling builds and git-clang-format precheck are failing. will push fixes soon.

edit: done. all checks passed.

@codegallivant codegallivant force-pushed the diags-raii branch 2 times, most recently from c2897ef to 72eed5a Compare March 12, 2026 23:16
Copy link
Copy Markdown
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

#endif
}

class DiagnosticsEngineRAII {
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

warning: class 'DiagnosticsEngineRAII' defines a non-default destructor but does not define a copy constructor, a copy assignment operator, a move constructor or a move assignment operator [cppcoreguidelines-special-member-functions]

class DiagnosticsEngineRAII {
      ^


class DiagnosticsEngineRAII {
private:
clang::DiagnosticsEngine& diags;
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

warning: member 'diags' of type 'clang::DiagnosticsEngine &' is a reference [cppcoreguidelines-avoid-const-or-ref-data-members]

  clang::DiagnosticsEngine& diags;
                            ^

clang::DiagnosticsEngine& diags;

public:
bool reset_condition; // additional condition to reset diagnostics
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

warning: member variable 'reset_condition' has public visibility [cppcoreguidelines-non-private-member-variables-in-classes]

  bool reset_condition; // additional condition to reset diagnostics
       ^

@codegallivant
Copy link
Copy Markdown
Contributor Author

@Vipul-Cariappa hello, would appreciate feedback about this, thanks.

Copy link
Copy Markdown
Collaborator

@Vipul-Cariappa Vipul-Cariappa left a comment

Choose a reason for hiding this comment

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

The way this is implemented, it looks more like a function call instead of a RAII object (Look at the way reset_condition is used). But I don't know if a simple function call is better or a RAII object is better...

@vgvassilev
Copy link
Copy Markdown
Contributor

The way this is implemented, it looks more like a function call instead of a RAII object (Look at the way reset_condition is used). But I don't know if a simple function call is better or a RAII object is better...

Wasn't the point here to use clang::Sema::SFINAETrap?

@codegallivant
Copy link
Copy Markdown
Contributor Author

Wasn't the point here to use clang::Sema::SFINAETrap?

Please refer to my analysis in this comment: #849 (comment)
I may be wrong, am new to this. Would appreciate feedback.

@vgvassilev
Copy link
Copy Markdown
Contributor

Wasn't the point here to use clang::Sema::SFINAETrap?

Please refer to my analysis in this comment: #849 (comment) I may be wrong, am new to this. Would appreciate feedback.

Did you try to implement it and how does it fail?

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.

3 participants