llvmorg-github-actions[bot] wrote:

<!--LLVM PR SUMMARY COMMENT-->

@llvm/pr-subscribers-clang-static-analyzer-1

Author: Arseniy Zaostrovnykh (necto)

<details>
<summary>Changes</summary>

`-fvalidate-ast-input-files-content` is silently ignored when loading PCH files 
for additional translation units.

This triggers an import failure when modification time of some of input files 
changes (for example, the AST dump is being reused for a subsequent 
cross-translation-unit re-analysis with Clang Static Analyzer after a fresh 
code checkout).
This makes it difficult to use cached AST dumps.

This patch enables the user to control validation of AST input files, without 
imposing it on them.

--
CPP-8025

---
Full diff: https://github.com/llvm/llvm-project/pull/196298.diff


2 Files Affected:

- (modified) clang/lib/Frontend/ASTUnit.cpp (+7-1) 
- (added) clang/test/Analysis/ctu/reusable-pch.c (+42) 


``````````diff
diff --git a/clang/lib/Frontend/ASTUnit.cpp b/clang/lib/Frontend/ASTUnit.cpp
index 05ae1f348f920..8003b72e0619d 100644
--- a/clang/lib/Frontend/ASTUnit.cpp
+++ b/clang/lib/Frontend/ASTUnit.cpp
@@ -780,11 +780,17 @@ std::unique_ptr<ASTUnit> ASTUnit::LoadFromASTFile(
       DisableValidationForModuleKind::None;
   if (::getenv("LIBCLANG_DISABLE_PCH_VALIDATION"))
     disableValid = DisableValidationForModuleKind::All;
+  bool ValidateASTInputFilesContent = HSOpts.ValidateASTInputFilesContent;
+
   AST->Reader = llvm::makeIntrusiveRefCnt<ASTReader>(
       *AST->PP, *AST->ModCache, AST->Ctx.get(), PCHContainerRdr,
       *AST->CodeGenOpts, ArrayRef<std::shared_ptr<ModuleFileExtension>>(),
       /*isysroot=*/"",
-      /*DisableValidationKind=*/disableValid, AllowASTWithCompilerErrors);
+      /*DisableValidationKind=*/disableValid, AllowASTWithCompilerErrors,
+      /*AllowConfigurationMismatch=*/ false,
+      /*ValidateSystemInputs=*/ false,
+      /*ForceValidateUserInputs=*/ false,
+      ValidateASTInputFilesContent);
 
   // Attach the AST reader to the AST context as an external AST source, so 
that
   // declarations will be deserialized from the AST file as needed.
diff --git a/clang/test/Analysis/ctu/reusable-pch.c 
b/clang/test/Analysis/ctu/reusable-pch.c
new file mode 100644
index 0000000000000..92256b9f0156c
--- /dev/null
+++ b/clang/test/Analysis/ctu/reusable-pch.c
@@ -0,0 +1,42 @@
+// RUN: rm -rf %t
+// RUN: mkdir -p %t
+// RUN: split-file %s %t
+
+// Step 1: Build PCH and defmap.
+// RUN: %clang_cc1 -x c -emit-pch -fvalidate-ast-input-files-content -o 
%t/other.c.ast %t/other.c
+// RUN: %clang_extdef_map %t/other.c -- -c -x c > %t/externalDefMap.tmp.txt
+// RUN: sed 's| .*other\.c| other.c.ast|' %t/externalDefMap.tmp.txt > 
%t/externalDefMap.txt
+
+// Step 2: Run CTU using the PCH - the division by zero is found via inlining.
+// RUN: %clang_cc1 -analyze \
+// RUN:   -fvalidate-ast-input-files-content \
+// RUN:   -analyzer-checker=core \
+// RUN:   -analyzer-config experimental-enable-naive-ctu-analysis=true \
+// RUN:   -analyzer-config ctu-dir=%t \
+// RUN:   -verify %t/main.c
+
+// Step 3: Advance mtime of the source from which PCH was built.
+// RUN: %python -c "import os, sys, time; os.utime(sys.argv[1], (time.time() + 
120, time.time() + 120))" %t/other.c
+
+// Step 4: Run CTU using the now-stale PCH - it breaks.
+// RUN: %clang_cc1 -analyze \
+// RUN:   -fvalidate-ast-input-files-content \
+// RUN:   -analyzer-checker=core \
+// RUN:   -analyzer-config experimental-enable-naive-ctu-analysis=true \
+// RUN:   -analyzer-config ctu-dir=%t \
+// RUN:   %t/main.c
+
+//--- main.c
+// Without CTU, always_zero() has an unknown return value so no bug is found.
+// With CTU, always_zero() is inlined and its return value (0) is known,
+// exposing the division by zero.
+
+int always_zero(void);
+
+void f(void) {
+  int x = always_zero();
+  (void)(1 / x); // expected-warning{{Division by zero}}
+}
+
+//--- other.c
+int always_zero(void) { return 0; }

``````````

</details>


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

Reply via email to