[flang-commits] [flang] [flang] Silence "used but undefined" warning for LOC(x) (PR #174807)

Peter Klausler via flang-commits flang-commits at lists.llvm.org
Wed Jan 7 09:21:14 PST 2026


https://github.com/klausler created https://github.com/llvm/llvm-project/pull/174807

When a variable appears as the argument of the extension intrinsic function LOC(x), assume that it's defined for the purposes of the warning about variables that are used but never defined, since the result of the LOC() can be used to define a based Cray pointer.

>From 751ab8f622086be18c132d9d384e13ea9c9cdffb Mon Sep 17 00:00:00 2001
From: Peter Klausler <pklausler at nvidia.com>
Date: Wed, 7 Jan 2026 09:14:25 -0800
Subject: [PATCH] [flang] Silence "used but undefined" warning for LOC(x)

When a variable appears as the argument of the extension intrinsic
function LOC(x), assume that it's defined for the purposes of the
warning about variables that are used but never defined, since the
result of the LOC() can be used to define a based Cray pointer.
---
 flang/lib/Semantics/check-call.cpp | 3 ++-
 flang/test/Semantics/bug2021.f90   | 8 ++++++++
 2 files changed, 10 insertions(+), 1 deletion(-)
 create mode 100644 flang/test/Semantics/bug2021.f90

diff --git a/flang/lib/Semantics/check-call.cpp b/flang/lib/Semantics/check-call.cpp
index 6470c3d0d09a9..54e1f6b3bb3fa 100644
--- a/flang/lib/Semantics/check-call.cpp
+++ b/flang/lib/Semantics/check-call.cpp
@@ -776,7 +776,8 @@ static void CheckExplicitDataArg(const characteristics::DummyDataObject &dummy,
         }
       }
     } else if (dummy.intent != common::Intent::In ||
-        (dummyIsPointer && !actualIsPointer)) {
+        (dummyIsPointer && !actualIsPointer) ||
+        (intrinsic && intrinsic->name == "loc")) {
       if (auto named{evaluate::ExtractNamedEntity(actual)}) {
         context.NoteDefinedSymbol(named->GetFirstSymbol());
       }
diff --git a/flang/test/Semantics/bug2021.f90 b/flang/test/Semantics/bug2021.f90
new file mode 100644
index 0000000000000..f5214bce946a9
--- /dev/null
+++ b/flang/test/Semantics/bug2021.f90
@@ -0,0 +1,8 @@
+!RUN: %flang -fc1 -fsyntax-only -pedantic %s 2>&1 | FileCheck %s
+!CHECK-NOT: warning: Value of uninitialized local variable 'b' is used but never defined [-Wused-undefined-variable]
+real :: a, b
+pointer(p,a)
+p = loc(b)
+a = 2.0
+print *, b
+end



More information about the flang-commits mailing list