Skip to content

[lldb] Reject redefinitions of persistent variables #1959

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
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
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
#include "lldb/Core/ModuleSpec.h"
#include "lldb/Core/ValueObjectConstResult.h"
#include "lldb/Core/ValueObjectVariable.h"
#include "lldb/Expression/DiagnosticManager.h"
#include "lldb/Expression/Materializer.h"
#include "lldb/Symbol/CompileUnit.h"
#include "lldb/Symbol/CompilerDecl.h"
Expand Down Expand Up @@ -125,6 +126,12 @@ void ClangExpressionDeclMap::InstallCodeGenerator(
m_parser_vars->m_code_gen = code_gen;
}

void ClangExpressionDeclMap::InstallDiagnosticManager(
DiagnosticManager &diag_manager) {
assert(m_parser_vars);
m_parser_vars->m_diagnostics = &diag_manager;
}

void ClangExpressionDeclMap::DidParse() {
if (m_parser_vars && m_parser_vars->m_persistent_vars) {
for (size_t entity_index = 0, num_entities = m_found_entities.GetSize();
Expand Down Expand Up @@ -196,6 +203,17 @@ bool ClangExpressionDeclMap::AddPersistentVariable(const NamedDecl *decl,
if (ast == nullptr)
return false;

// Check if we already declared a persistent variable with the same name.
if (lldb::ExpressionVariableSP conflicting_var =
m_parser_vars->m_persistent_vars->GetVariable(name)) {
std::string msg = llvm::formatv("redefinition of persistent variable '{0}'",
name).str();
m_parser_vars->m_diagnostics->AddDiagnostic(
msg, DiagnosticSeverity::eDiagnosticSeverityError,
DiagnosticOrigin::eDiagnosticOriginLLDB);
return false;
}

if (m_parser_vars->m_materializer && is_result) {
Status err;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,8 @@ class ClangExpressionDeclMap : public ClangASTSource {

void InstallCodeGenerator(clang::ASTConsumer *code_gen);

void InstallDiagnosticManager(DiagnosticManager &diag_manager);

/// Disable the state needed for parsing and IR transformation.
void DidParse();

Expand Down Expand Up @@ -330,6 +332,8 @@ class ClangExpressionDeclMap : public ClangASTSource {
clang::ASTConsumer *m_code_gen = nullptr; ///< If non-NULL, a code generator
///that receives new top-level
///functions.
DiagnosticManager *m_diagnostics = nullptr;

private:
ParserVars(const ParserVars &) = delete;
const ParserVars &operator=(const ParserVars &) = delete;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1074,6 +1074,7 @@ ClangExpressionParser::ParseInternal(DiagnosticManager &diagnostic_manager,
ClangExpressionDeclMap *decl_map = type_system_helper->DeclMap();
if (decl_map) {
decl_map->InstallCodeGenerator(&m_compiler->getASTConsumer());
decl_map->InstallDiagnosticManager(diagnostic_manager);

clang::ExternalASTSource *ast_source = decl_map->CreateProxy();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,3 +41,19 @@ def test_persistent_variables(self):
# Test that $200 wasn't created by the previous expression.
self.expect("expr $200", error=True,
substrs=["use of undeclared identifier '$200'"])

# Try redeclaring the persistent variable with the same type.
# This should be rejected as we treat them as if they are globals.
self.expect("expr int $i = 123", error=True,
substrs=["error: redefinition of persistent variable '$i'"])
self.expect_expr("$i", result_type="int", result_value="5")

# Try redeclaring the persistent variable with another type. Should
# also be rejected.
self.expect("expr long $i = 123", error=True,
substrs=["error: redefinition of persistent variable '$i'"])
self.expect_expr("$i", result_type="int", result_value="5")

# Try assigning the persistent variable a new value.
self.expect("expr $i = 55")
self.expect_expr("$i", result_type="int", result_value="55")