[clang] [Clang] Diagnose apply AST consume actions on LLVM IR (PR #88602)

via cfe-commits cfe-commits at lists.llvm.org
Tue Apr 16 05:06:30 PDT 2024


https://github.com/yronglin updated https://github.com/llvm/llvm-project/pull/88602

>From 9d0656461683c4a6af76aa3990def06b04eb1b0a Mon Sep 17 00:00:00 2001
From: yronglin <yronglin777 at gmail.com>
Date: Sat, 13 Apr 2024 15:46:36 +0800
Subject: [PATCH 1/2] [Clang] Diagnose apply AST consume actions on LLVM IR

Signed-off-by: yronglin <yronglin777 at gmail.com>
---
 .../clang/Basic/DiagnosticFrontendKinds.td    |  3 ++
 clang/lib/Frontend/FrontendAction.cpp         |  7 +++--
 clang/test/Frontend/ast-dump-on-llvm.ll       | 29 +++++++++++++++++++
 3 files changed, 37 insertions(+), 2 deletions(-)
 create mode 100644 clang/test/Frontend/ast-dump-on-llvm.ll

diff --git a/clang/include/clang/Basic/DiagnosticFrontendKinds.td b/clang/include/clang/Basic/DiagnosticFrontendKinds.td
index 14b08d4927ec5e..2cf4ddfa4e805d 100644
--- a/clang/include/clang/Basic/DiagnosticFrontendKinds.td
+++ b/clang/include/clang/Basic/DiagnosticFrontendKinds.td
@@ -370,4 +370,7 @@ def warn_missing_symbol_graph_dir : Warning<
   "Missing symbol graph output directory, defaulting to working directory">,
   InGroup<ExtractAPIMisuse>;
 
+def err_ast_action_on_unparsable_source : Error<
+  "can not apply ast actions to LLVM IR file '%0'">, 
+  DefaultFatal;
 }
diff --git a/clang/lib/Frontend/FrontendAction.cpp b/clang/lib/Frontend/FrontendAction.cpp
index b7c9967316f0b8..4d5a6aaf0b5f71 100644
--- a/clang/lib/Frontend/FrontendAction.cpp
+++ b/clang/lib/Frontend/FrontendAction.cpp
@@ -757,8 +757,11 @@ bool FrontendAction::BeginSourceFile(CompilerInstance &CI,
 
   // IR files bypass the rest of initialization.
   if (Input.getKind().getLanguage() == Language::LLVM_IR) {
-    assert(hasIRSupport() &&
-           "This action does not have IR file support!");
+    if (!hasIRSupport()) {
+      CI.getDiagnostics().Report(diag::err_ast_action_on_unparsable_source)
+          << Input.getFile();
+      return false;
+    }
 
     // Inform the diagnostic client we are processing a source file.
     CI.getDiagnosticClient().BeginSourceFile(CI.getLangOpts(), nullptr);
diff --git a/clang/test/Frontend/ast-dump-on-llvm.ll b/clang/test/Frontend/ast-dump-on-llvm.ll
new file mode 100644
index 00000000000000..72aafc361cde84
--- /dev/null
+++ b/clang/test/Frontend/ast-dump-on-llvm.ll
@@ -0,0 +1,29 @@
+; RUN: not %clang_cc1 -triple x86_64-unknown-unknown -ast-dump %s -o - 2>&1 | FileCheck %s --check-prefix=CHECK-AST-DUMP
+; RUN: not %clang_cc1 -triple x86_64-unknown-unknown -ast-dump=json %s -o - 2>&1 | FileCheck %s --check-prefix=CHECK-AST-DUMP-EQ-JSON
+; RUN: not %clang_cc1 -triple x86_64-unknown-unknown -ast-dump=default %s -o - 2>&1 | FileCheck %s --check-prefix=CHECK-AST-DUMP-EQ-DEFAULT
+; RUN: not %clang_cc1 -triple x86_64-unknown-unknown -ast-dump-all %s -o - 2>&1 | FileCheck %s --check-prefix=CHECK-AST-DUMP-ALL
+; RUN: not %clang_cc1 -triple x86_64-unknown-unknown -ast-dump-all=json %s -o - 2>&1 | FileCheck %s --check-prefix=CHECK-AST-DUMP-ALL-EQ-JSON
+; RUN: not %clang_cc1 -triple x86_64-unknown-unknown -ast-dump-all=default %s -o - 2>&1 | FileCheck %s --check-prefix=CHECK-AST-DUMP-ALL-EQ-DEFAULT
+
+; RUN: not %clang_cc1 -triple x86_64-unknown-unknown -ast-print %s -o - 2>&1 | FileCheck %s --check-prefix=CHECK-AST-PRINT
+; RUN: not %clang_cc1 -triple x86_64-unknown-unknown -ast-view %s -o - 2>&1 | FileCheck %s --check-prefix=CHECK-AST-VIEW
+; RUN: not %clang_cc1 -triple x86_64-unknown-unknown -ast-list %s -o - 2>&1 | FileCheck %s --check-prefix=CHECK-AST-LIST
+; RUN: not %clang_cc1 -triple x86_64-unknown-unknown -ast-dump-lookups %s -o - 2>&1 | FileCheck %s --check-prefix=CHECK-AST-DUMP-LOOKUP
+; RUN: not %clang_cc1 -triple x86_64-unknown-unknown -ast-dump-filter=FunctionDecl %s -o - 2>&1 | FileCheck %s --check-prefix=CHECK-AST-DUMP-FILTER-EQ
+; RUN: not %clang_cc1 -triple x86_64-unknown-unknown -ast-dump-decl-types %s -o - 2>&1 | FileCheck %s --check-prefix=CHECK-AST-DUMP-DECL-TYPES
+; RUN: not %clang_cc1 -triple x86_64-unknown-unknown -fsyntax-only %s -o - 2>&1 | FileCheck %s --check-prefix=CHECK-SYNTAX-ONLY
+
+
+; CHECK-AST-DUMP: fatal error: can not apply ast actions to LLVM IR file '{{.*}}'
+; CHECK-AST-DUMP-EQ-JSON: fatal error: can not apply ast actions to LLVM IR file '{{.*}}'
+; CHECK-AST-DUMP-EQ-DEFAULT: fatal error: can not apply ast actions to LLVM IR file '{{.*}}'
+; CHECK-AST-DUMP-ALL: fatal error: can not apply ast actions to LLVM IR file '{{.*}}'
+; CHECK-AST-DUMP-ALL-EQ-JSON: fatal error: can not apply ast actions to LLVM IR file '{{.*}}'
+; CHECK-AST-DUMP-ALL-EQ-DEFAULT: fatal error: can not apply ast actions to LLVM IR file '{{.*}}'
+; CHECK-AST-PRINT: fatal error: can not apply ast actions to LLVM IR file '{{.*}}'
+; CHECK-AST-VIEW: fatal error: can not apply ast actions to LLVM IR file '{{.*}}'
+; CHECK-AST-LIST: fatal error: can not apply ast actions to LLVM IR file '{{.*}}'
+; CHECK-AST-DUMP-LOOKUP: fatal error: can not apply ast actions to LLVM IR file '{{.*}}'
+; CHECK-AST-DUMP-FILTER-EQ: fatal error: can not apply ast actions to LLVM IR file '{{.*}}'
+; CHECK-AST-DUMP-DECL-TYPES: fatal error: can not apply ast actions to LLVM IR file '{{.*}}'
+; CHECK-SYNTAX-ONLY: fatal error: can not apply ast actions to LLVM IR file '{{.*}}'

>From 2df35668cce38c07446c2314d55fe1d4bc295dc7 Mon Sep 17 00:00:00 2001
From: yronglin <yronglin777 at gmail.com>
Date: Tue, 16 Apr 2024 20:06:17 +0800
Subject: [PATCH 2/2] Address review comments

Signed-off-by: yronglin <yronglin777 at gmail.com>
---
 .../clang/Basic/DiagnosticFrontendKinds.td    |  4 +--
 clang/lib/Frontend/FrontendAction.cpp         |  2 +-
 clang/test/Frontend/ast-dump-on-llvm.ll       | 26 +++++++++----------
 3 files changed, 16 insertions(+), 16 deletions(-)

diff --git a/clang/include/clang/Basic/DiagnosticFrontendKinds.td b/clang/include/clang/Basic/DiagnosticFrontendKinds.td
index 2cf4ddfa4e805d..becf349640cd4e 100644
--- a/clang/include/clang/Basic/DiagnosticFrontendKinds.td
+++ b/clang/include/clang/Basic/DiagnosticFrontendKinds.td
@@ -370,7 +370,7 @@ def warn_missing_symbol_graph_dir : Warning<
   "Missing symbol graph output directory, defaulting to working directory">,
   InGroup<ExtractAPIMisuse>;
 
-def err_ast_action_on_unparsable_source : Error<
-  "can not apply ast actions to LLVM IR file '%0'">, 
+def err_ast_action_on_llvm_ir : Error<
+  "cannot apply AST actions to LLVM IR file '%0'">, 
   DefaultFatal;
 }
diff --git a/clang/lib/Frontend/FrontendAction.cpp b/clang/lib/Frontend/FrontendAction.cpp
index 4d5a6aaf0b5f71..26ff944c4c6002 100644
--- a/clang/lib/Frontend/FrontendAction.cpp
+++ b/clang/lib/Frontend/FrontendAction.cpp
@@ -758,7 +758,7 @@ bool FrontendAction::BeginSourceFile(CompilerInstance &CI,
   // IR files bypass the rest of initialization.
   if (Input.getKind().getLanguage() == Language::LLVM_IR) {
     if (!hasIRSupport()) {
-      CI.getDiagnostics().Report(diag::err_ast_action_on_unparsable_source)
+      CI.getDiagnostics().Report(diag::err_ast_action_on_llvm_ir)
           << Input.getFile();
       return false;
     }
diff --git a/clang/test/Frontend/ast-dump-on-llvm.ll b/clang/test/Frontend/ast-dump-on-llvm.ll
index 72aafc361cde84..cdacfde4ba848c 100644
--- a/clang/test/Frontend/ast-dump-on-llvm.ll
+++ b/clang/test/Frontend/ast-dump-on-llvm.ll
@@ -14,16 +14,16 @@
 ; RUN: not %clang_cc1 -triple x86_64-unknown-unknown -fsyntax-only %s -o - 2>&1 | FileCheck %s --check-prefix=CHECK-SYNTAX-ONLY
 
 
-; CHECK-AST-DUMP: fatal error: can not apply ast actions to LLVM IR file '{{.*}}'
-; CHECK-AST-DUMP-EQ-JSON: fatal error: can not apply ast actions to LLVM IR file '{{.*}}'
-; CHECK-AST-DUMP-EQ-DEFAULT: fatal error: can not apply ast actions to LLVM IR file '{{.*}}'
-; CHECK-AST-DUMP-ALL: fatal error: can not apply ast actions to LLVM IR file '{{.*}}'
-; CHECK-AST-DUMP-ALL-EQ-JSON: fatal error: can not apply ast actions to LLVM IR file '{{.*}}'
-; CHECK-AST-DUMP-ALL-EQ-DEFAULT: fatal error: can not apply ast actions to LLVM IR file '{{.*}}'
-; CHECK-AST-PRINT: fatal error: can not apply ast actions to LLVM IR file '{{.*}}'
-; CHECK-AST-VIEW: fatal error: can not apply ast actions to LLVM IR file '{{.*}}'
-; CHECK-AST-LIST: fatal error: can not apply ast actions to LLVM IR file '{{.*}}'
-; CHECK-AST-DUMP-LOOKUP: fatal error: can not apply ast actions to LLVM IR file '{{.*}}'
-; CHECK-AST-DUMP-FILTER-EQ: fatal error: can not apply ast actions to LLVM IR file '{{.*}}'
-; CHECK-AST-DUMP-DECL-TYPES: fatal error: can not apply ast actions to LLVM IR file '{{.*}}'
-; CHECK-SYNTAX-ONLY: fatal error: can not apply ast actions to LLVM IR file '{{.*}}'
+; CHECK-AST-DUMP: fatal error: cannot apply AST actions to LLVM IR file '{{.*}}'
+; CHECK-AST-DUMP-EQ-JSON: fatal error: cannot apply AST actions to LLVM IR file '{{.*}}'
+; CHECK-AST-DUMP-EQ-DEFAULT: fatal error: cannot apply AST actions to LLVM IR file '{{.*}}'
+; CHECK-AST-DUMP-ALL: fatal error: cannot apply AST actions to LLVM IR file '{{.*}}'
+; CHECK-AST-DUMP-ALL-EQ-JSON: fatal error: cannot apply AST actions to LLVM IR file '{{.*}}'
+; CHECK-AST-DUMP-ALL-EQ-DEFAULT: fatal error: cannot apply AST actions to LLVM IR file '{{.*}}'
+; CHECK-AST-PRINT: fatal error: cannot apply AST actions to LLVM IR file '{{.*}}'
+; CHECK-AST-VIEW: fatal error: cannot apply AST actions to LLVM IR file '{{.*}}'
+; CHECK-AST-LIST: fatal error: cannot apply AST actions to LLVM IR file '{{.*}}'
+; CHECK-AST-DUMP-LOOKUP: fatal error: cannot apply AST actions to LLVM IR file '{{.*}}'
+; CHECK-AST-DUMP-FILTER-EQ: fatal error: cannot apply AST actions to LLVM IR file '{{.*}}'
+; CHECK-AST-DUMP-DECL-TYPES: fatal error: cannot apply AST actions to LLVM IR file '{{.*}}'
+; CHECK-SYNTAX-ONLY: fatal error: cannot apply AST actions to LLVM IR file '{{.*}}'



More information about the cfe-commits mailing list