[clang] [Lexer] Prevent hitting the file system for ASTReader tokens (PR #192492)

via cfe-commits cfe-commits at lists.llvm.org
Sun Apr 19 05:31:26 PDT 2026


================
@@ -0,0 +1,15 @@
+// RUN: rm -f %t.h %t.pch
+// RUN: echo "int variable_del_cern = 42;" > %t.h
+// RUN: %clang_cc1 -x c++-header -emit-pch -o %t.pch %t.h
+// RUN: rm %t.h
+// RUN: %clang_cc1 -fno-validate-pch -ast-dump -include-pch %t.pch %s
+
+// This test ensures that Clang does not access the filesystem when
+// handling SourceLocations originating from a PCH.
+//
+// After generating the PCH, the original header is removed. If Clang
+// attempts to access it (e.g. via MeasureTokenLength), the test will fail.
+
+int function() { 
----------------
100475224 wrote:

Yes, absolutely. Without the patch in Lexer.cpp, the test fails.

When -ast-dump forces the traversal of the AST, Clang attempts to calculate the token lengths by calling Lexer::getLocForEndOfToken, which eventually falls back to the physical file system. Since the test explicitly deletes the underlying header (rm %t.h) before running the dumper, the unpatched Lexer hits the missing file and triggers a fatal 'file not found' error.

The patch successfully prevents this by catching the loaded source location early.

https://github.com/llvm/llvm-project/pull/192492


More information about the cfe-commits mailing list