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

[tcling] Suppress -Wunused-result diagnostics in wrappers generated by TClingCallFunc #9244

Merged
merged 3 commits into from
Nov 7, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions core/metacling/src/TClingCallFunc.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -982,7 +982,7 @@ void TClingCallFunc::make_narg_call_with_return(const unsigned N, const string &
// new (ret) (return_type) ((class_name*)obj)->func(args...);
// }
// else {
// ((class_name*)obj)->func(args...);
// (void)(((class_name*)obj)->func(args...));
// }
//
const FunctionDecl *FD = GetDecl();
Expand Down Expand Up @@ -1085,8 +1085,9 @@ void TClingCallFunc::make_narg_call_with_return(const unsigned N, const string &
for (int i = 0; i < indent_level; ++i) {
callbuf << kIndentString;
}
callbuf << "(void)(";
make_narg_call(type_name, N, typedefbuf, callbuf, class_name, indent_level);
callbuf << ";\n";
callbuf << ");\n";
for (int i = 0; i < indent_level; ++i) {
callbuf << kIndentString;
}
Expand Down
34 changes: 34 additions & 0 deletions core/metacling/test/TClingCallFuncTests.cxx
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
#include "ROOTUnitTestSupport.h"
#include "TInterpreter.h"

#include "gtest/gtest.h"
Expand Down Expand Up @@ -259,3 +260,36 @@ TEST(TClingCallFunc, DISABLED_OverloadedTemplate)
}
)cpp");
}

TEST(TClingCallFunc, FunctionWrapperNodiscard)
{
gInterpreter->Declare(R"cpp(
struct TClingCallFunc_Nodiscard1 {
#if __cplusplus >= 201703L
[[nodiscard]]
#endif
bool foo(int) { return false; }
};
)cpp");

ClassInfo_t *FooNamespace = gInterpreter->ClassInfo_Factory("TClingCallFunc_Nodiscard1");
CallFunc_t *mc = gInterpreter->CallFunc_Factory();
Longptr_t offset = 0;

gInterpreter->CallFunc_SetFuncProto(mc, FooNamespace, "foo", "int", &offset);
std::string wrapper = gInterpreter->CallFunc_GetWrapperCode(mc);

{
using ::testing::Not;
using ::testing::HasSubstr;
FilterDiagsRAII RAII([] (int /*level*/, Bool_t /*abort*/,
const char * /*location*/, const char *msg) {
EXPECT_THAT(msg, Not(HasSubstr("-Wunused-result")));
});
ASSERT_TRUE(gInterpreter->Declare(wrapper.c_str()));
}

// Cleanup
gInterpreter->CallFunc_Delete(mc);
gInterpreter->ClassInfo_Delete(FooNamespace);
}
24 changes: 24 additions & 0 deletions test/unit_testing_support/ROOTUnitTestSupport.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,9 @@
#ifndef ROOT_UNITTESTSUPPORT_H
#define ROOT_UNITTESTSUPPORT_H

#include "TError.h"
#include "TInterpreter.h"

#include "gtest/gtest.h"
#include "gmock/gmock.h"

Expand All @@ -27,6 +30,27 @@ using testing::StrEq;
using testing::internal::GetCapturedStderr;
using testing::internal::CaptureStderr;
using testing::internal::RE;

/// \brief Allows a user function to filter ROOT/cling diagnostics, e.g.
/// ```c++
/// FilterDiagsRAII RAII([] (int level, Bool_t abort,
/// const char *location, const char *msg) {
/// EXPECT_THAT(msg, Not(HasSubstr("-Wunused-result")));
/// });
/// ```
class FilterDiagsRAII {
ErrorHandlerFunc_t fPrevHandler;
public:
FilterDiagsRAII(ErrorHandlerFunc_t fn) : fPrevHandler(::GetErrorHandler()) {
::SetErrorHandler(fn);
gInterpreter->ReportDiagnosticsToErrorHandler();
}
~FilterDiagsRAII() {
gInterpreter->ReportDiagnosticsToErrorHandler(/*enable=*/false);
::SetErrorHandler(fPrevHandler);
}
};

class ExpectedDiagRAII {
public:
enum ExpectedDiagKind {
Expand Down