[llvm] 671f0e2 - [clang] Make libBasic not depend on MC

Nico Weber via llvm-commits llvm-commits at lists.llvm.org
Wed Apr 28 09:16:40 PDT 2021


Author: Nico Weber
Date: 2021-04-28T12:16:22-04:00
New Revision: 671f0e2e189c561512511331d95de382e2d6d15d

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

LOG: [clang] Make libBasic not depend on MC

Reduces numbers of files built for clang-format from 575 to 449.

Requires two small changes:

1. Don't use llvm::ExceptionHandling in LangOptions. This isn't
   even quite the right type since we don't use all of its values.
   Tweaks the changes made in:
   - https://reviews.llvm.org/D93215
   - https://reviews.llvm.org/D93216

2. Move section name validation code added (long ago) in commit 30ba67439 out
   of libBasic into Sema and base the check on the triple. This is a bit less
   OOP-y, but completely in line with what we do in many other places in Sema.

No behavior change.

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

Added: 
    

Modified: 
    clang/include/clang/Basic/DiagnosticFrontendKinds.td
    clang/include/clang/Basic/LangOptions.h
    clang/include/clang/Basic/TargetInfo.h
    clang/include/clang/Driver/Options.td
    clang/include/clang/Sema/Sema.h
    clang/lib/Basic/CMakeLists.txt
    clang/lib/Basic/Targets/OSTargets.h
    clang/lib/Frontend/CompilerInvocation.cpp
    clang/lib/Sema/CMakeLists.txt
    clang/lib/Sema/SemaAttr.cpp
    clang/lib/Sema/SemaDeclAttr.cpp
    llvm/utils/gn/secondary/clang/lib/Basic/BUILD.gn
    llvm/utils/gn/secondary/clang/lib/Sema/BUILD.gn
    llvm/utils/gn/secondary/clang/tools/clang-format/BUILD.gn

Removed: 
    


################################################################################
diff  --git a/clang/include/clang/Basic/DiagnosticFrontendKinds.td b/clang/include/clang/Basic/DiagnosticFrontendKinds.td
index 39fdefc77fbee..5122770316cde 100644
--- a/clang/include/clang/Basic/DiagnosticFrontendKinds.td
+++ b/clang/include/clang/Basic/DiagnosticFrontendKinds.td
@@ -112,7 +112,7 @@ def err_fe_action_not_available : Error<
 def err_fe_invalid_alignment : Error<
     "invalid value '%1' in '%0'; alignment must be a power of 2">;
 def err_fe_invalid_exception_model
-   : Error<"invalid exception model '%select{none|dwarf|sjlj|arm|seh|wasm|aix}0' for target '%1'">;
+   : Error<"invalid exception model '%select{none|sjlj|seh|dwarf|wasm}0' for target '%1'">;
 def warn_fe_concepts_ts_flag : Warning<
   "-fconcepts-ts is deprecated - use '-std=c++20' for Concepts support">,
   InGroup<Deprecated>;

diff  --git a/clang/include/clang/Basic/LangOptions.h b/clang/include/clang/Basic/LangOptions.h
index c898ef19a6cfd..85fe4af720235 100644
--- a/clang/include/clang/Basic/LangOptions.h
+++ b/clang/include/clang/Basic/LangOptions.h
@@ -23,7 +23,6 @@
 #include "llvm/ADT/FloatingPointMode.h"
 #include "llvm/ADT/StringRef.h"
 #include "llvm/ADT/Triple.h"
-#include "llvm/MC/MCTargetOptions.h"
 #include <string>
 #include <vector>
 
@@ -222,7 +221,7 @@ class LangOptions : public LangOptionsBase {
   };
 
   /// Possible exception handling behavior.
-  using ExceptionHandlingKind = llvm::ExceptionHandling;
+  enum class ExceptionHandlingKind { None, SjLj, WinEH, DwarfCFI, Wasm };
 
   enum class LaxVectorConversionKind {
     /// Permit no implicit vector bitcasts.
@@ -410,19 +409,19 @@ class LangOptions : public LangOptionsBase {
   }
 
   bool hasSjLjExceptions() const {
-    return getExceptionHandling() == llvm::ExceptionHandling::SjLj;
+    return getExceptionHandling() == ExceptionHandlingKind::SjLj;
   }
 
   bool hasSEHExceptions() const {
-    return getExceptionHandling() == llvm::ExceptionHandling::WinEH;
+    return getExceptionHandling() == ExceptionHandlingKind::WinEH;
   }
 
   bool hasDWARFExceptions() const {
-    return getExceptionHandling() == llvm::ExceptionHandling::DwarfCFI;
+    return getExceptionHandling() == ExceptionHandlingKind::DwarfCFI;
   }
 
   bool hasWasmExceptions() const {
-    return getExceptionHandling() == llvm::ExceptionHandling::Wasm;
+    return getExceptionHandling() == ExceptionHandlingKind::Wasm;
   }
 };
 

diff  --git a/clang/include/clang/Basic/TargetInfo.h b/clang/include/clang/Basic/TargetInfo.h
index 3300fe012aa81..532ff4554656c 100644
--- a/clang/include/clang/Basic/TargetInfo.h
+++ b/clang/include/clang/Basic/TargetInfo.h
@@ -1147,21 +1147,6 @@ class TargetInfo : public virtual TransferrableTargetInfo,
             getTriple().getVendor() == llvm::Triple::SCEI);
   }
 
-  /// An optional hook that targets can implement to perform semantic
-  /// checking on attribute((section("foo"))) specifiers.
-  ///
-  /// In this case, "foo" is passed in to be checked.  If the section
-  /// specifier is invalid, the backend should return an Error that indicates
-  /// the problem.
-  ///
-  /// This hook is a simple quality of implementation feature to catch errors
-  /// and give good diagnostics in cases when the assembler or code generator
-  /// would otherwise reject the section specifier.
-  ///
-  virtual llvm::Error isValidSectionSpecifier(StringRef SR) const {
-    return llvm::Error::success();
-  }
-
   /// Set forced language options.
   ///
   /// Apply changes to the target information with respect to certain

diff  --git a/clang/include/clang/Driver/Options.td b/clang/include/clang/Driver/Options.td
index 06a21a9b6e9ca..1b4dfe4b6d3bd 100644
--- a/clang/include/clang/Driver/Options.td
+++ b/clang/include/clang/Driver/Options.td
@@ -1387,7 +1387,7 @@ def fwasm_exceptions : Flag<["-"], "fwasm-exceptions">, Group<f_Group>,
 def exception_model : Separate<["-"], "exception-model">,
   Flags<[CC1Option, NoDriverOption]>, HelpText<"The exception model: dwarf|sjlj|seh|wasm">,
   Values<"dwarf,sjlj,seh,wasm">,
-  NormalizedValuesScope<"llvm::ExceptionHandling">,
+  NormalizedValuesScope<"LangOptions::ExceptionHandlingKind">,
   NormalizedValues<["DwarfCFI", "SjLj", "WinEH", "Wasm"]>,
   MarshallingInfoEnum<LangOpts<"ExceptionHandling">, "None">;
 def exception_model_EQ : Joined<["-"], "exception-model=">,

diff  --git a/clang/include/clang/Sema/Sema.h b/clang/include/clang/Sema/Sema.h
index e0ededea6bcae..aeb05d758cae6 100644
--- a/clang/include/clang/Sema/Sema.h
+++ b/clang/include/clang/Sema/Sema.h
@@ -4288,6 +4288,7 @@ class Sema final {
   bool checkStringLiteralArgumentAttr(const ParsedAttr &Attr, unsigned ArgNum,
                                       StringRef &Str,
                                       SourceLocation *ArgLocation = nullptr);
+  llvm::Error isValidSectionSpecifier(StringRef Str);
   bool checkSectionName(SourceLocation LiteralLoc, StringRef Str);
   bool checkTargetAttr(SourceLocation LiteralLoc, StringRef Str);
   bool checkMSInheritanceAttrOnDefinition(

diff  --git a/clang/lib/Basic/CMakeLists.txt b/clang/lib/Basic/CMakeLists.txt
index 534886e8ed2e7..cbf99b2a848f2 100644
--- a/clang/lib/Basic/CMakeLists.txt
+++ b/clang/lib/Basic/CMakeLists.txt
@@ -1,5 +1,4 @@
 set(LLVM_LINK_COMPONENTS
-  MC
   Support
   )
 

diff  --git a/clang/lib/Basic/Targets/OSTargets.h b/clang/lib/Basic/Targets/OSTargets.h
index 6e757adfa8bfd..cecac1cec172a 100644
--- a/clang/lib/Basic/Targets/OSTargets.h
+++ b/clang/lib/Basic/Targets/OSTargets.h
@@ -13,7 +13,6 @@
 #define LLVM_CLANG_LIB_BASIC_TARGETS_OSTARGETS_H
 
 #include "Targets.h"
-#include "llvm/MC/MCSectionMachO.h"
 
 namespace clang {
 namespace targets {
@@ -114,15 +113,6 @@ class LLVM_LIBRARY_VISIBILITY DarwinTargetInfo : public OSTargetInfo<Target> {
     this->MCountName = "\01mcount";
   }
 
-  llvm::Error isValidSectionSpecifier(StringRef SR) const override {
-    // Let MCSectionMachO validate this.
-    StringRef Segment, Section;
-    unsigned TAA, StubSize;
-    bool HasTAA;
-    return llvm::MCSectionMachO::ParseSectionSpecifier(SR, Segment, Section,
-                                                       TAA, HasTAA, StubSize);
-  }
-
   const char *getStaticInitSectionSpecifier() const override {
     // FIXME: We should return 0 when building kexts.
     return "__TEXT,__StaticInit,regular,pure_instructions";

diff  --git a/clang/lib/Frontend/CompilerInvocation.cpp b/clang/lib/Frontend/CompilerInvocation.cpp
index 36d2fe3795282..2578c8cc0b952 100644
--- a/clang/lib/Frontend/CompilerInvocation.cpp
+++ b/clang/lib/Frontend/CompilerInvocation.cpp
@@ -463,7 +463,8 @@ static bool FixupInvocation(CompilerInvocation &Invocation,
 
   CodeGenOpts.CodeModel = TargetOpts.CodeModel;
 
-  if (LangOpts.getExceptionHandling() != llvm::ExceptionHandling::None &&
+  if (LangOpts.getExceptionHandling() !=
+          LangOptions::ExceptionHandlingKind::None &&
       T.isWindowsMSVCEnvironment())
     Diags.Report(diag::err_fe_invalid_exception_model)
         << static_cast<unsigned>(LangOpts.getExceptionHandling()) << T.str();

diff  --git a/clang/lib/Sema/CMakeLists.txt b/clang/lib/Sema/CMakeLists.txt
index 251856c933aca..0e0681a8e2927 100644
--- a/clang/lib/Sema/CMakeLists.txt
+++ b/clang/lib/Sema/CMakeLists.txt
@@ -1,6 +1,7 @@
 set(LLVM_LINK_COMPONENTS
   Core
   FrontendOpenMP
+  MC
   Support
   )
 

diff  --git a/clang/lib/Sema/SemaAttr.cpp b/clang/lib/Sema/SemaAttr.cpp
index 67e553a3b2594..fe8f02f023685 100644
--- a/clang/lib/Sema/SemaAttr.cpp
+++ b/clang/lib/Sema/SemaAttr.cpp
@@ -269,8 +269,10 @@ void Sema::ActOnPragmaOptionsAlign(PragmaOptionsAlignKind Kind,
   AlignPackStack.Act(PragmaLoc, Action, StringRef(), Info);
 }
 
-void Sema::ActOnPragmaClangSection(SourceLocation PragmaLoc, PragmaClangSectionAction Action,
-                                   PragmaClangSectionKind SecKind, StringRef SecName) {
+void Sema::ActOnPragmaClangSection(SourceLocation PragmaLoc,
+                                   PragmaClangSectionAction Action,
+                                   PragmaClangSectionKind SecKind,
+                                   StringRef SecName) {
   PragmaClangSection *CSec;
   int SectionFlags = ASTContext::PSF_Read;
   switch (SecKind) {
@@ -301,8 +303,7 @@ void Sema::ActOnPragmaClangSection(SourceLocation PragmaLoc, PragmaClangSectionA
     return;
   }
 
-  if (llvm::Error E =
-          Context.getTargetInfo().isValidSectionSpecifier(SecName)) {
+  if (llvm::Error E = isValidSectionSpecifier(SecName)) {
     Diag(PragmaLoc, diag::err_pragma_section_invalid_for_target)
         << toString(std::move(E));
     CSec->Valid = false;

diff  --git a/clang/lib/Sema/SemaDeclAttr.cpp b/clang/lib/Sema/SemaDeclAttr.cpp
index 7db720f6c57d3..f961076421b5b 100644
--- a/clang/lib/Sema/SemaDeclAttr.cpp
+++ b/clang/lib/Sema/SemaDeclAttr.cpp
@@ -40,6 +40,7 @@
 #include "llvm/ADT/STLExtras.h"
 #include "llvm/ADT/StringExtras.h"
 #include "llvm/IR/Assumptions.h"
+#include "llvm/MC/MCSectionMachO.h"
 #include "llvm/Support/Error.h"
 #include "llvm/Support/MathExtras.h"
 #include "llvm/Support/raw_ostream.h"
@@ -2985,9 +2986,29 @@ SectionAttr *Sema::mergeSectionAttr(Decl *D, const AttributeCommonInfo &CI,
   return ::new (Context) SectionAttr(Context, CI, Name);
 }
 
+/// Used to implement to perform semantic checking on
+/// attribute((section("foo"))) specifiers.
+///
+/// In this case, "foo" is passed in to be checked.  If the section
+/// specifier is invalid, return an Error that indicates the problem.
+///
+/// This is a simple quality of implementation feature to catch errors
+/// and give good diagnostics in cases when the assembler or code generator
+/// would otherwise reject the section specifier.
+llvm::Error Sema::isValidSectionSpecifier(StringRef SecName) {
+  if (!Context.getTargetInfo().getTriple().isOSDarwin())
+    return llvm::Error::success();
+
+  // Let MCSectionMachO validate this.
+  StringRef Segment, Section;
+  unsigned TAA, StubSize;
+  bool HasTAA;
+  return llvm::MCSectionMachO::ParseSectionSpecifier(SecName, Segment, Section,
+                                                     TAA, HasTAA, StubSize);
+}
+
 bool Sema::checkSectionName(SourceLocation LiteralLoc, StringRef SecName) {
-  if (llvm::Error E =
-          Context.getTargetInfo().isValidSectionSpecifier(SecName)) {
+  if (llvm::Error E = isValidSectionSpecifier(SecName)) {
     Diag(LiteralLoc, diag::err_attribute_section_invalid_for_target)
         << toString(std::move(E)) << 1 /*'section'*/;
     return false;
@@ -3021,8 +3042,7 @@ static void handleSectionAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
 // `#pragma code_seg("segname")` uses checkSectionName() instead.
 static bool checkCodeSegName(Sema &S, SourceLocation LiteralLoc,
                              StringRef CodeSegName) {
-  if (llvm::Error E =
-          S.Context.getTargetInfo().isValidSectionSpecifier(CodeSegName)) {
+  if (llvm::Error E = S.isValidSectionSpecifier(CodeSegName)) {
     S.Diag(LiteralLoc, diag::err_attribute_section_invalid_for_target)
         << toString(std::move(E)) << 0 /*'code-seg'*/;
     return false;

diff  --git a/llvm/utils/gn/secondary/clang/lib/Basic/BUILD.gn b/llvm/utils/gn/secondary/clang/lib/Basic/BUILD.gn
index 0db0cb25b7de0..52d38954c3c1d 100644
--- a/llvm/utils/gn/secondary/clang/lib/Basic/BUILD.gn
+++ b/llvm/utils/gn/secondary/clang/lib/Basic/BUILD.gn
@@ -49,7 +49,6 @@ static_library("Basic") {
     "//clang/include/clang/Sema:AttrParsedAttrKinds",
     "//clang/include/clang/Sema:AttrSpellingListIndex",
     "//llvm/include/llvm/Config:llvm-config",
-    "//llvm/lib/MC",
     "//llvm/lib/Support",
   ]
   include_dirs = [ "." ]

diff  --git a/llvm/utils/gn/secondary/clang/lib/Sema/BUILD.gn b/llvm/utils/gn/secondary/clang/lib/Sema/BUILD.gn
index 8ec471e130e32..93d6b7dc2d29a 100644
--- a/llvm/utils/gn/secondary/clang/lib/Sema/BUILD.gn
+++ b/llvm/utils/gn/secondary/clang/lib/Sema/BUILD.gn
@@ -25,6 +25,7 @@ static_library("Sema") {
     "//clang/lib/Edit",
     "//clang/lib/Lex",
     "//llvm/lib/Frontend/OpenMP",
+    "//llvm/lib/MC",
     "//llvm/lib/Support",
   ]
   sources = [

diff  --git a/llvm/utils/gn/secondary/clang/tools/clang-format/BUILD.gn b/llvm/utils/gn/secondary/clang/tools/clang-format/BUILD.gn
index 243dae89bb6a6..d1a83faa63229 100644
--- a/llvm/utils/gn/secondary/clang/tools/clang-format/BUILD.gn
+++ b/llvm/utils/gn/secondary/clang/tools/clang-format/BUILD.gn
@@ -12,6 +12,7 @@ executable("clang-format") {
     "//clang/lib/Frontend/",
     "//clang/lib/Sema/",
     "//llvm/lib/IR",
+    "//llvm/lib/MC",
   ]
   sources = [ "ClangFormat.cpp" ]
 }


        


More information about the llvm-commits mailing list