[clang] c1c7230 - [clang][deps] Prevent emitting diagnostics outside of source file

Jan Svoboda via cfe-commits cfe-commits at lists.llvm.org
Tue Oct 11 15:57:30 PDT 2022


Author: Jan Svoboda
Date: 2022-10-11T15:57:20-07:00
New Revision: c1c72302fe4553a39be37b5c310d046975e55019

URL: https://github.com/llvm/llvm-project/commit/c1c72302fe4553a39be37b5c310d046975e55019
DIFF: https://github.com/llvm/llvm-project/commit/c1c72302fe4553a39be37b5c310d046975e55019.diff

LOG: [clang][deps] Prevent emitting diagnostics outside of source file

The dependency scanner needs to report the module map file describing the module whose implementation is being compiled (see D134222). However, calling `Preprocessor::getCurrentModuleImplementation()` in the scanner might cause a diagnostic during module map parsing and emitting a diagnostic without being "in" a source file is illegal (e.g. in `TextDiagnosticPrinter`). This patch ensures the module map parse is triggered while the compiler is still "in" a source file, avoiding the failure case.

Reviewed By: Bigcheese

Differential Revision: https://reviews.llvm.org/D135637

Added: 
    clang/test/ClangScanDeps/modules-implementation-module-map.c

Modified: 
    clang/lib/Frontend/FrontendAction.cpp

Removed: 
    


################################################################################
diff  --git a/clang/lib/Frontend/FrontendAction.cpp b/clang/lib/Frontend/FrontendAction.cpp
index b541c59fb9c4d..45f04eac818da 100644
--- a/clang/lib/Frontend/FrontendAction.cpp
+++ b/clang/lib/Frontend/FrontendAction.cpp
@@ -914,6 +914,9 @@ bool FrontendAction::BeginSourceFile(CompilerInstance &CI,
       CI.getDiagnostics().Report(diag::err_module_map_not_found) << Filename;
   }
 
+  // If compiling implementation of a module, load its module map file now.
+  (void)CI.getPreprocessor().getCurrentModuleImplementation();
+
   // Add a module declaration scope so that modules from -fmodule-map-file
   // arguments may shadow modules found implicitly in search paths.
   CI.getPreprocessor()

diff  --git a/clang/test/ClangScanDeps/modules-implementation-module-map.c b/clang/test/ClangScanDeps/modules-implementation-module-map.c
new file mode 100644
index 0000000000000..d76d315700469
--- /dev/null
+++ b/clang/test/ClangScanDeps/modules-implementation-module-map.c
@@ -0,0 +1,38 @@
+// RUN: rm -rf %t
+// RUN: split-file %s %t
+
+//--- cdb.json.template
+[{
+  "file": "DIR/tu.m",
+  "directory": "DIR",
+  "command": "clang -fmodules -fimplicit-module-maps -fmodules-cache-path=DIR/cache -fmodule-name=FWPrivate -c DIR/tu.m -o DIR/tu.o -F DIR/frameworks -Wprivate-module"
+}]
+
+//--- frameworks/FW.framework/Modules/module.modulemap
+framework module FW {}
+//--- frameworks/FW.framework/Modules/module.private.modulemap
+// The module name will trigger a diagnostic.
+framework module FWPrivate { header "private.h" }
+//--- frameworks/FW.framework/PrivateHeaders/private.h
+//--- tu.m
+
+// RUN: sed -e "s|DIR|%/t|g" %t/cdb.json.template > %t/cdb.json
+// RUN: clang-scan-deps -compilation-database %t/cdb.json -format experimental-full > %t/result.json
+// RUN: cat %t/result.json | sed 's:\\\\\?:/:g' | FileCheck %s -DPREFIX=%/t
+// CHECK:      "translation-units": [
+// CHECK-NEXT:   {
+// CHECK-NEXT:     "commands": [
+// CHECK:            {
+// CHECK:              "command-line": [
+// CHECK:                "-fmodule-map-file=[[PREFIX]]/frameworks/FW.framework/Modules/module.private.modulemap",
+// CHECK:                "-fmodule-name=FWPrivate",
+// CHECK:              ],
+// CHECK-NEXT:         "executable": "clang",
+// CHECK-NEXT:         "file-deps": [
+// CHECK-NEXT:           "[[PREFIX]]/tu.m"
+// CHECK-NEXT:         ],
+// CHECK-NEXT:         "input-file": "[[PREFIX]]/tu.m"
+// CHECK-NEXT:       }
+// CHECK:          ]
+// CHECK:        }
+// CHECK:      ]


        


More information about the cfe-commits mailing list