Skip to content

Commit

Permalink
[cling] Add deprecation warning for declarations without auto (root…
Browse files Browse the repository at this point in the history
…-project#14645)

* [cling] Add deprecation warning for declarations without `auto`

Declarations without the auto keyword are not part of standard C++. Even
though it is a nice feature to have, it requires a patch on top of clang
and is one of the hurdles preventing us from using the upstream clang.

Implicit auto injection is currently only supported at the prompt (and
only in the top-most function-level scope). So it should ideally not break
other features.

There are a few GitHub and JIRA issues related to this feature that can
also be closed if we completely remove it.

* Set severity for diag::warn_deprecated_message manually
  • Loading branch information
devajithvs authored Mar 14, 2024
1 parent 74a1027 commit 1034db6
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 0 deletions.
7 changes: 7 additions & 0 deletions core/metacling/src/TClingCallbacks.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
#include "clang/Sema/Scope.h"
#include "clang/Serialization/ASTReader.h"
#include "clang/Serialization/GlobalModuleIndex.h"
#include "clang/Basic/DiagnosticSema.h"

#include "llvm/ExecutionEngine/Orc/Core.h"

Expand Down Expand Up @@ -1004,6 +1005,12 @@ bool TClingCallbacks::tryInjectImplicitAutoKeyword(LookupResult &R, Scope *S) {
Result->addAttr(AnnotateAttr::CreateImplicit(C, "__Auto"));

R.addDecl(Result);

// Raise a warning when trying to use implicit auto injection feature.
SemaRef.getDiagnostics().setSeverity(diag::warn_deprecated_message, diag::Severity::Warning, SourceLocation());
SemaRef.Diag(Loc, diag::warn_deprecated_message)
<< "declaration without the 'auto' keyword" << DC << Loc << FixItHint::CreateInsertion(Loc, "auto ");

// Say that we can handle the situation. Clang should try to recover
return true;
}
Expand Down
13 changes: 13 additions & 0 deletions core/metacling/test/TClingTests.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -239,6 +239,19 @@ TEST_F(TClingTests, GetSharedLibDeps)
}
#endif

// Check that a warning message is generated when using auto-injection.
TEST_F(TClingTests, WarningAutoInjection)
{
ROOT::TestSupport::CheckDiagsRAII diags;
diags.requiredDiag(kWarning, "cling", "declaration without the 'auto' keyword is deprecated",
/*matchFullMessage=*/false);

gInterpreter->ProcessLine("/* no auto */ t = new int;");

auto t = gInterpreter->GetDataMember(nullptr, "t");
EXPECT_TRUE(t != nullptr);
}

// Check the interface which interacts with the cling::LookupHelper.
TEST_F(TClingTests, ClingLookupHelper) {
// Exception spec evaluation.
Expand Down

0 comments on commit 1034db6

Please sign in to comment.