From bf04bad2c43a69730e1cdf4dcd2d06ab786bda33 Mon Sep 17 00:00:00 2001
From: luofeng14 <luofeng13@huawei.com>
Date: Wed, 17 Apr 2024 10:49:19 +0800
Subject: [PATCH] Fix declaration definition mismatch for classic flang

---
 .../clang/Basic/DiagnosticDriverKinds.td      |  4 ++++
 clang/include/clang/Basic/MacroBuilder.h      |  7 ++++++-
 clang/include/clang/Basic/Sanitizers.def      |  2 ++
 clang/include/clang/Driver/Options.td         |  1 -
 clang/include/clang/Frontend/Utils.h          | 10 ++++++++-
 clang/lib/CodeGen/CGDebugInfo.cpp             | 21 +++++++++++++++----
 clang/lib/Driver/Driver.cpp                   |  1 -
 clang/lib/Frontend/InitPreprocessor.cpp       | 21 +++++++++++++++++++
 .../test/CodeGen/libpgmath-logfun-aarch64.ll  |  1 +
 clang/test/CodeGen/libpgmath-logfun-x86_64.ll |  2 +-
 clang/test/Driver/autocomplete.c              |  2 +-
 .../flang/classic-flang-vscale-mbits.f95      |  2 ++
 .../Driver/flang/classic-flang-vscale.f95     |  2 ++
 clang/tools/driver/CMakeLists.txt             |  6 +++++-
 14 files changed, 71 insertions(+), 11 deletions(-)

diff --git a/clang/include/clang/Basic/DiagnosticDriverKinds.td b/clang/include/clang/Basic/DiagnosticDriverKinds.td
index 09a1949d7596..62a60e65c6a4 100644
--- a/clang/include/clang/Basic/DiagnosticDriverKinds.td
+++ b/clang/include/clang/Basic/DiagnosticDriverKinds.td
@@ -131,10 +131,12 @@ def err_drv_invalid_linker_name : Error<
   "invalid linker name in argument '%0'">;
 def err_drv_invalid_rtlib_name : Error<
   "invalid runtime library name in argument '%0'">;
+#ifdef ENABLE_CLASSIC_FLANG
 def err_drv_invalid_allocatable_mode : Error<
   "invalid semantic mode for assignments to allocatables in argument '%0'">;
 def err_drv_unsupported_fixed_line_length : Error<
   "unsupported fixed-format line length in argument '%0'">;
+#endif
 def err_drv_unsupported_rtlib_for_platform : Error<
   "unsupported runtime library '%0' for platform '%1'">;
 def err_drv_invalid_unwindlib_name : Error<
@@ -358,8 +360,10 @@ def err_drv_negative_columns : Error<
   "invalid value '%1' in '%0', value must be 'none' or a positive integer">;
 def err_drv_small_columns : Error<
   "invalid value '%1' in '%0', value must be '%2' or greater">;
+#ifdef ENABLE_CLASSIC_FLANG
 def err_drv_clang_unsupported_minfo_arg : Error<
   "'%0' option does not support '%1' value">;
+#endif
 
 def err_drv_invalid_malign_branch_EQ : Error<
   "invalid argument '%0' to -malign-branch=; each element must be one of: %1">;
diff --git a/clang/include/clang/Basic/MacroBuilder.h b/clang/include/clang/Basic/MacroBuilder.h
index bfc5e38c15e3..54e9ea643394 100644
--- a/clang/include/clang/Basic/MacroBuilder.h
+++ b/clang/include/clang/Basic/MacroBuilder.h
@@ -24,10 +24,15 @@ class MacroBuilder {
   raw_ostream &Out;
 public:
   MacroBuilder(raw_ostream &Output) : Out(Output) {}
+  #ifdef ENABLE_CLASSIC_FLANG
   virtual ~MacroBuilder() {}
-
+  #endif
   /// Append a \#define line for macro of the form "\#define Name Value\n".
+  #ifdef ENABLE_CLASSIC_FLANG
   virtual void defineMacro(const Twine &Name, const Twine &Value = "1") {
+  #else
+  void defineMacro(const Twine &Name, const Twine &Value = "1") {
+  #endif
     Out << "#define " << Name << ' ' << Value << '\n';
   }
 
diff --git a/clang/include/clang/Basic/Sanitizers.def b/clang/include/clang/Basic/Sanitizers.def
index 4f0a2bf332ef..a8fcba677ee5 100644
--- a/clang/include/clang/Basic/Sanitizers.def
+++ b/clang/include/clang/Basic/Sanitizers.def
@@ -107,8 +107,10 @@ SANITIZER("signed-integer-overflow", SignedIntegerOverflow)
 SANITIZER("unreachable", Unreachable)
 SANITIZER("vla-bound", VLABound)
 SANITIZER("vptr", Vptr)
+#ifdef ENABLE_CLASSIC_FLANG
 // fortran contiguous pointer checks
 SANITIZER("discontiguous", Discontiguous)
+#endif
 
 // IntegerSanitizer
 SANITIZER("unsigned-integer-overflow", UnsignedIntegerOverflow)
diff --git a/clang/include/clang/Driver/Options.td b/clang/include/clang/Driver/Options.td
index 365285966a67..dab35f22e4ac 100644
--- a/clang/include/clang/Driver/Options.td
+++ b/clang/include/clang/Driver/Options.td
@@ -5473,7 +5473,6 @@ defm stack_arrays : BoolOptionWithoutMarshalling<"f", "stack-arrays",
 defm loop_versioning : BoolOptionWithoutMarshalling<"f", "version-loops-for-stride",
   PosFlag<SetTrue, [], "Create unit-strided versions of loops">,
    NegFlag<SetFalse, [], "Do not create unit-strided loops (default)">>;
-
 } // let Flags = [FC1Option, FlangOption, FlangOnlyOption]
 
 #ifdef ENABLE_CLASSIC_FLANG
diff --git a/clang/include/clang/Frontend/Utils.h b/clang/include/clang/Frontend/Utils.h
index c51c617c1379..4ac76dd348c6 100644
--- a/clang/include/clang/Frontend/Utils.h
+++ b/clang/include/clang/Frontend/Utils.h
@@ -17,10 +17,12 @@
 #include "clang/Basic/LLVM.h"
 #include "clang/Driver/OptionUtils.h"
 #include "clang/Frontend/DependencyOutputOptions.h"
+#ifdef ENABLE_CLASSIC_FLANG
 #include "clang/Basic/TargetInfo.h"
+#include "llvm/ADT/StringExtras.h"
+#endif
 #include "llvm/ADT/ArrayRef.h"
 #include "llvm/ADT/IntrusiveRefCntPtr.h"
-#include "llvm/ADT/StringExtras.h"
 #include "llvm/ADT/StringMap.h"
 #include "llvm/ADT/StringRef.h"
 #include "llvm/ADT/StringSet.h"
@@ -33,11 +35,13 @@
 #include <utility>
 #include <vector>
 
+#ifdef ENABLE_CLASSIC_FLANG
 namespace llvm {
 
 class StringRef;
 
 } // namespace llvm
+#endif
 
 namespace clang {
 
@@ -47,7 +51,9 @@ class CompilerInvocation;
 class DiagnosticsEngine;
 class ExternalSemaSource;
 class FrontendOptions;
+#ifdef ENABLE_CLASSIC_FLANG
 class MacroBuilder;
+#endif
 class PCHContainerReader;
 class Preprocessor;
 class PreprocessorOptions;
@@ -63,6 +69,7 @@ void InitializePreprocessor(Preprocessor &PP, const PreprocessorOptions &PPOpts,
 void DoPrintPreprocessedInput(Preprocessor &PP, raw_ostream *OS,
                               const PreprocessorOutputOptions &Opts);
 
+#ifdef ENABLE_CLASSIC_FLANG
 /// DefineTypeSize - Emit a macro to the predefines buffer that declares a macro
 /// named MacroName with the max value for a type with width 'TypeWidth' a
 /// signedness of 'isSigned' and with a value suffix of 'ValSuffix' (e.g. LL).
@@ -85,6 +92,7 @@ static void DefineTypeSize(const Twine &MacroName, TargetInfo::IntType Ty,
   DefineTypeSize(MacroName, TI.getTypeWidth(Ty), TI.getTypeConstantSuffix(Ty),
                  TI.isTypeSigned(Ty), Builder);
 }
+#endif
 
 /// An interface for collecting the dependencies of a compilation. Users should
 /// use \c attachToPreprocessor and \c attachToASTReader to get all of the
diff --git a/clang/lib/CodeGen/CGDebugInfo.cpp b/clang/lib/CodeGen/CGDebugInfo.cpp
index 3b140860cce4..30d62ae8faf6 100644
--- a/clang/lib/CodeGen/CGDebugInfo.cpp
+++ b/clang/lib/CodeGen/CGDebugInfo.cpp
@@ -3981,7 +3981,10 @@ CGDebugInfo::getGlobalVariableForwardDeclaration(const VarDecl *VD) {
   auto *GV = DBuilder.createTempGlobalVariableFwdDecl(
       DContext, Name, LinkageName, Unit, Line, getOrCreateType(T, Unit),
       !VD->isExternallyVisible(), nullptr, TemplateParameters,
-      llvm::DINode::FlagZero, Align);
+#ifdef ENABLE_CLASSIC_FLANG
+      llvm::DINode::FlagZero,
+#endif
+      Align);
   FwdDeclReplaceMap.emplace_back(
       std::piecewise_construct,
       std::make_tuple(cast<VarDecl>(VD->getCanonicalDecl())),
@@ -5468,7 +5471,10 @@ void CGDebugInfo::EmitGlobalVariable(llvm::GlobalVariable *Var,
         Var->hasLocalLinkage(), true,
         Expr.empty() ? nullptr : DBuilder.createExpression(Expr),
         getOrCreateStaticDataMemberDeclarationOrNull(D), TemplateParameters,
-        llvm::DINode::FlagZero, Align, Annotations);
+#ifdef ENABLE_CLASSIC_FLANG
+        llvm::DINode::FlagZero,
+#endif
+        Align, Annotations);
     Var->addDebugInfo(GVE);
   }
   DeclCache[D->getCanonicalDecl()].reset(GVE);
@@ -5564,7 +5570,11 @@ void CGDebugInfo::EmitGlobalVariable(const ValueDecl *VD, const APValue &Init) {
   GV.reset(DBuilder.createGlobalVariableExpression(
       DContext, Name, StringRef(), Unit, getLineNumber(VD->getLocation()), Ty,
       true, true, InitExpr, getOrCreateStaticDataMemberDeclarationOrNull(VarD),
-      TemplateParameters, llvm::DINode::FlagZero, Align));
+      TemplateParameters,
+#ifdef ENABLE_CLASSIC_FLANG
+      llvm::DINode::FlagZero,
+#endif
+      Align));
 }
 
 void CGDebugInfo::EmitExternalVariable(llvm::GlobalVariable *Var,
@@ -5582,7 +5592,10 @@ void CGDebugInfo::EmitExternalVariable(llvm::GlobalVariable *Var,
   llvm::DIGlobalVariableExpression *GVE =
       DBuilder.createGlobalVariableExpression(
           DContext, Name, StringRef(), Unit, getLineNumber(D->getLocation()),
-          Ty, false, false, nullptr, nullptr, nullptr, llvm::DINode::FlagZero,
+          Ty, false, false, nullptr, nullptr, nullptr,
+#ifdef ENABLE_CLASSIC_FLANG
+          llvm::DINode::FlagZero,
+#endif
           Align);
   Var->addDebugInfo(GVE);
 }
diff --git a/clang/lib/Driver/Driver.cpp b/clang/lib/Driver/Driver.cpp
index 8bfee5928fe5..819d7703b2e7 100644
--- a/clang/lib/Driver/Driver.cpp
+++ b/clang/lib/Driver/Driver.cpp
@@ -377,7 +377,6 @@ phases::ID Driver::getFinalPhase(const DerivedArgList &DAL,
              (PhaseArg = DAL.getLastArg(options::OPT_fmodule_header,
                                         options::OPT_fmodule_header_EQ))) {
     FinalPhase = phases::Precompile;
-
     // -{fsyntax-only,-analyze,emit-ast} only run up to the compiler.
   } else if ((PhaseArg = DAL.getLastArg(options::OPT_fsyntax_only)) ||
              (PhaseArg = DAL.getLastArg(options::OPT_print_supported_cpus)) ||
diff --git a/clang/lib/Frontend/InitPreprocessor.cpp b/clang/lib/Frontend/InitPreprocessor.cpp
index 7b01cb01a45e..05682ae9f135 100644
--- a/clang/lib/Frontend/InitPreprocessor.cpp
+++ b/clang/lib/Frontend/InitPreprocessor.cpp
@@ -161,6 +161,27 @@ static void DefineFloatMacros(MacroBuilder &Builder, StringRef Prefix,
   Builder.defineMacro(DefPrefix + "MIN__", Twine(Min)+Ext);
 }
 
+#ifndef ENABLE_CLASSIC_FLANG
+/// DefineTypeSize - Emit a macro to the predefines buffer that declares a macro
+/// named MacroName with the max value for a type with width 'TypeWidth' a
+/// signedness of 'isSigned' and with a value suffix of 'ValSuffix' (e.g. LL).
+static void DefineTypeSize(const Twine &MacroName, unsigned TypeWidth,
+                           StringRef ValSuffix, bool isSigned,
+                           MacroBuilder &Builder) {
+  llvm::APInt MaxVal = isSigned ? llvm::APInt::getSignedMaxValue(TypeWidth)
+                                : llvm::APInt::getMaxValue(TypeWidth);
+  Builder.defineMacro(MacroName, toString(MaxVal, 10, isSigned) + ValSuffix);
+}
+
+/// DefineTypeSize - An overloaded helper that uses TargetInfo to determine
+/// the width, suffix, and signedness of the given type
+static void DefineTypeSize(const Twine &MacroName, TargetInfo::IntType Ty,
+                           const TargetInfo &TI, MacroBuilder &Builder) {
+  DefineTypeSize(MacroName, TI.getTypeWidth(Ty), TI.getTypeConstantSuffix(Ty),
+                 TI.isTypeSigned(Ty), Builder);
+}
+#endif
+
 static void DefineFmt(const Twine &Prefix, TargetInfo::IntType Ty,
                       const TargetInfo &TI, MacroBuilder &Builder) {
   bool IsSigned = TI.isTypeSigned(Ty);
diff --git a/clang/test/CodeGen/libpgmath-logfun-aarch64.ll b/clang/test/CodeGen/libpgmath-logfun-aarch64.ll
index 141fed29ccd1..be05a472016c 100644
--- a/clang/test/CodeGen/libpgmath-logfun-aarch64.ll
+++ b/clang/test/CodeGen/libpgmath-logfun-aarch64.ll
@@ -1,3 +1,4 @@
+; REQUIRES: classic_flang
 ; REQUIRES: aarch64-registered-target
 
 ; RUN: %clang -target aarch64-unknown-linux-gnu -Ofast -S %s -o - | FileCheck %s
diff --git a/clang/test/CodeGen/libpgmath-logfun-x86_64.ll b/clang/test/CodeGen/libpgmath-logfun-x86_64.ll
index 3ce1d910947f..c89e538f34e3 100644
--- a/clang/test/CodeGen/libpgmath-logfun-x86_64.ll
+++ b/clang/test/CodeGen/libpgmath-logfun-x86_64.ll
@@ -1,5 +1,5 @@
 ; REQUIRES: x86-registered-target
-
+; REQUIRES: classic_flang
 ; RUN: %clang -target x86_64-unknown-linux-gnu -msse -Ofast -S %s -o - | FileCheck %s
 
 target datalayout = "e-m:e-p270:32:32-p271:32:32-p272:64:64-i64:64-f80:128-n8:16:32:64-S128"
diff --git a/clang/test/Driver/autocomplete.c b/clang/test/Driver/autocomplete.c
index 8c4921c9dbd5..d6f57708b67e 100644
--- a/clang/test/Driver/autocomplete.c
+++ b/clang/test/Driver/autocomplete.c
@@ -85,7 +85,7 @@
 // FVECLIBALL-NEXT: libmvec
 // FVECLIBALL-NEXT: MASSV
 // FVECLIBALL-NEXT: none
-// FVECLIBALL:      SLEEF
+// FVECLIBALL-NEXT: SLEEF
 // FVECLIBALL-NEXT: SVML
 // RUN: %clang --autocomplete=-fshow-overloads= | FileCheck %s -check-prefix=FSOVERALL
 // FSOVERALL: all
diff --git a/clang/test/Driver/flang/classic-flang-vscale-mbits.f95 b/clang/test/Driver/flang/classic-flang-vscale-mbits.f95
index f0ed43aa027a..089993be7b04 100644
--- a/clang/test/Driver/flang/classic-flang-vscale-mbits.f95
+++ b/clang/test/Driver/flang/classic-flang-vscale-mbits.f95
@@ -1,3 +1,5 @@
+! REQUIRES: classic_flang
+
 // RUN: %clang --driver-mode=flang -### -S --target=aarch64 -march=armv8-a+sve -msve-vector-bits=128 %s 2>&1 | FileCheck -check-prefix=CHECK-SVE-128 %s
 // RUN: %clang --driver-mode=flang -### -S --target=aarch64 -march=armv8-a+sve -msve-vector-bits=128+ %s 2>&1 | FileCheck -check-prefix=CHECK-SVE-128PLUS %s
 // RUN: %clang --driver-mode=flang -### -S --target=aarch64 -march=armv8-a+sve -msve-vector-bits=256 %s 2>&1 | FileCheck -check-prefix=CHECK-SVE-256 %s
diff --git a/clang/test/Driver/flang/classic-flang-vscale.f95 b/clang/test/Driver/flang/classic-flang-vscale.f95
index 8110be594db5..b8496975758f 100644
--- a/clang/test/Driver/flang/classic-flang-vscale.f95
+++ b/clang/test/Driver/flang/classic-flang-vscale.f95
@@ -1,3 +1,5 @@
+! REQUIRES: classic_flang
+
 // RUN: %clang --driver-mode=flang -### -S --target=aarch64 -march=armv8-a %s 2>&1 | FileCheck -check-prefix=CHECK-NEON %s
 // RUN: %clang --driver-mode=flang -### -S --target=aarch64 -march=armv8-a+sve %s 2>&1 | FileCheck -check-prefix=CHECK-SVE %s
 // RUN: %clang --driver-mode=flang -### -S --target=aarch64 -march=armv8-a+sve2 %s 2>&1 | FileCheck -check-prefix=CHECK-SVE2 %s
diff --git a/clang/tools/driver/CMakeLists.txt b/clang/tools/driver/CMakeLists.txt
index c269561fde69..465b05c9c0e9 100644
--- a/clang/tools/driver/CMakeLists.txt
+++ b/clang/tools/driver/CMakeLists.txt
@@ -59,7 +59,11 @@ endif()
 add_dependencies(clang clang-resource-headers)
 
 if(NOT CLANG_LINKS_TO_CREATE)
-  set(CLANG_LINKS_TO_CREATE clang++ clang-cl clang-cpp flang)
+    if (LLVM_ENABLE_CLASSIC_FLANG)
+       set(CLANG_LINKS_TO_CREATE clang++ clang-cl clang-cpp flang)
+    else()
+       set(CLANG_LINKS_TO_CREATE clang++ clang-cl clang-cpp)
+    endif()
 endif()
 
 if (CLANG_ENABLE_HLSL)
-- 
2.24.3 (Apple Git-128)

