https://github.com/guitargeek created 
https://github.com/llvm/llvm-project/pull/240108

To decide whether an input is a declaration or a statement, the parser 
classifies the input's first names at file scope. For `f();` with an undeclared 
`f`, that leaves an annot_non_type_undeclared token behind. The 
TopLevelStmtDecl that is parsed next has a function scope of its own, but it 
never looks `f` up again.

Clients can resolve names in that scope through an ExternalSemaSource. ROOT 
does that for objects and functions that only exist at runtime, like objects 
read from a file. It resolves them only in statements, not at file scope. With 
a source that knows `dynamic_fn` only in function scopes, such a statement 
failed:
```
  Interp->Parse("dynamic_fn();");
  input_line_1:1:1: error: use of undeclared identifier 'dynamic_fn'
```

Turn the annotation back into the identifier, so that the statement looks the 
name up in its own scope.

Assisted-by: Claude Opus 5.5

This is another PR that addresses a problem when using Clang-Repl instead of 
Cling in ROOT.

>From 193359c08758c8319a418bfa5507413e759cc717 Mon Sep 17 00:00:00 2001
From: Jonas Rembser <[email protected]>
Date: Thu, 1 Oct 2026 20:18:28 +0000
Subject: [PATCH] [clang-repl] Look up first name of a top-level statement in
 its scope

To decide whether an input is a declaration or a statement, the parser
classifies its first names at file scope. For `f();` with an undeclared
`f`, that leaves an annot_non_type_undeclared token behind, and the
TopLevelStmtDecl that is parsed next never looks `f` up again, although
it has a function scope of its own.

Clients can resolve names in that scope through an ExternalSemaSource
(ROOT finds the names of objects and functions that only exist at
runtime, but only in statements, not at file scope). Turn the
annotation back into the identifier, so that the statement looks it up.

Assisted-by: Claude Opus 5.5
---
 clang/lib/Parse/Parser.cpp                    | 13 ++++++-
 .../unittests/Interpreter/InterpreterTest.cpp | 38 +++++++++++++++++++
 2 files changed, 50 insertions(+), 1 deletion(-)

diff --git a/clang/lib/Parse/Parser.cpp b/clang/lib/Parse/Parser.cpp
index bf13cdc2f9aca5..4d23f1b85ca98e 100644
--- a/clang/lib/Parse/Parser.cpp
+++ b/clang/lib/Parse/Parser.cpp
@@ -1004,8 +1004,19 @@ Parser::ParseExternalDeclaration(ParsedAttributes &Attrs,
       return nullptr;
     }
     if (getLangOpts().IncrementalExtensions &&
-        !isDeclarationStatement(/*DisambiguatingWithExpression=*/true))
+        !isDeclarationStatement(/*DisambiguatingWithExpression=*/true)) {
+      // The disambiguation looked up the first name at file scope. The
+      // statement has a scope of its own, where an ExternalSemaSource may
+      // know a name that is undeclared at file scope (`f();`). Look it up
+      // again.
+      if (Tok.is(tok::annot_non_type_undeclared)) {
+        IdentifierInfo *II = getIdentifierAnnotation(Tok);
+        Tok.setKind(tok::identifier);
+        Tok.setIdentifierInfo(II);
+        Tok.setLength(II->getLength());
+      }
       return ParseTopLevelStmtDecl();
+    }
 
     // We can't tell whether this is a function-definition or declaration yet.
     if (!SingleDecl)
diff --git a/clang/unittests/Interpreter/InterpreterTest.cpp 
b/clang/unittests/Interpreter/InterpreterTest.cpp
index 41c1facc8f2f08..bcc8cd5c0d0c65 100644
--- a/clang/unittests/Interpreter/InterpreterTest.cpp
+++ b/clang/unittests/Interpreter/InterpreterTest.cpp
@@ -19,12 +19,15 @@
 #include "clang/Frontend/TextDiagnosticPrinter.h"
 #include "clang/Interpreter/Interpreter.h"
 #include "clang/Interpreter/Value.h"
+#include "clang/Sema/ExternalSemaSource.h"
 #include "clang/Sema/Lookup.h"
+#include "clang/Sema/Scope.h"
 #include "clang/Sema/Sema.h"
 
 #include "llvm/IR/Function.h"
 #include "llvm/IR/Module.h"
 #include "llvm/TargetParser/Host.h"
+#include "llvm/Testing/Support/Error.h"
 
 #include "gmock/gmock.h"
 #include "gtest/gtest.h"
@@ -688,4 +691,39 @@ TEST_F(InterpreterTest, EmscriptenExceptionHandling) {
       Interp->ParseAndExecute("try { throw 1; } catch (...) { 0; }"));
 }
 
+// Knows `dynamic_fn` (an `int()`), but only inside function scopes, like
+// clients that resolve names of statements at runtime.
+class FunctionScopeSource : public ExternalSemaSource {
+public:
+  unsigned FileScopeLookups = 0;
+  bool LookupUnqualified(LookupResult &R, Scope *S) override {
+    if (!R.getLookupName().isIdentifier() ||
+        R.getLookupName().getAsIdentifierInfo()->getName() != "dynamic_fn")
+      return false;
+    if (!S || !S->getFnParent()) {
+      ++FileScopeLookups;
+      return false;
+    }
+    ASTContext &C = R.getSema().getASTContext();
+    QualType FT = C.getFunctionType(C.IntTy, {}, {});
+    auto *FD = FunctionDecl::Create(
+        C, C.getTranslationUnitDecl(), SourceLocation(), SourceLocation(),
+        R.getLookupName(), FT, C.getTrivialTypeSourceInfo(FT), SC_Extern);
+    R.addDecl(FD);
+    return true;
+  }
+};
+
+TEST_F(InterpreterTest, TopLevelStmtLooksUpFirstNameInItsScope) {
+  std::unique_ptr<Interpreter> Interp = createInterpreter();
+  auto Source = llvm::makeIntrusiveRefCnt<FunctionScopeSource>();
+  Interp->getCompilerInstance()->getSema().addExternalSource(Source);
+
+  // Deciding whether this is a declaration looks up `dynamic_fn` at file
+  // scope; the statement must look it up again in its own scope.
+  EXPECT_THAT_EXPECTED(Interp->Parse("dynamic_fn();"), llvm::Succeeded());
+  EXPECT_GT(Source->FileScopeLookups, 0U);
+  EXPECT_THAT_EXPECTED(Interp->Parse("int x = dynamic_fn();"), llvm::Failed());
+}
+
 } // end anonymous namespace

_______________________________________________
cfe-commits mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits

Reply via email to