[clang] [clang][NFC] Introduce `SemaBase` (PR #87634)

2024-04-06 Thread Vlad Serebrennikov via cfe-commits

https://github.com/Endilll closed 
https://github.com/llvm/llvm-project/pull/87634
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [clang][NFC] Introduce `SemaBase` (PR #87634)

2024-04-05 Thread Vlad Serebrennikov via cfe-commits

https://github.com/Endilll updated 
https://github.com/llvm/llvm-project/pull/87634

>From 311e2ef14dda46686b473e813028a2c3b2ac1254 Mon Sep 17 00:00:00 2001
From: Vlad Serebrennikov 
Date: Thu, 4 Apr 2024 16:07:35 +0300
Subject: [PATCH] [clang][NFC] Introduce `SemaBase`

This is a follow-up to #84184. Multiple reviewers there pointed out to me that 
we should have a common base class for `Sema` and `SemaOpenACC` to avoid code 
duplication for common helpers like `getLangOpts`. On top of that, `Diag()` 
function was requested for `SemaOpenACC`. This patch delivers both.

The intent is to keep `SemaBase` as small as possible, as things there are 
globally available across `Sema` and its parts without any additional effort 
from usage side. Overused, this can undermine the whole endeavor of splitting 
`Sema` apart.

Apart of shuffling code around, this patch introduces a helper private function 
`SemaDiagnosticBuilder::getDeviceDeferredDiags()`, the sole purpose of which is 
to encapsulate member access into (incomplete) `Sema` for function templates 
defined in the header, where `Sema` can't be complete.
---
 clang/include/clang/Sema/Sema.h| 204 +-
 clang/include/clang/Sema/SemaBase.h| 224 +
 clang/include/clang/Sema/SemaOpenACC.h |  14 +-
 clang/lib/Sema/CMakeLists.txt  |   1 +
 clang/lib/Sema/Sema.cpp|  45 +
 clang/lib/Sema/SemaBase.cpp|  85 ++
 clang/lib/Sema/SemaOpenACC.cpp |  16 +-
 7 files changed, 329 insertions(+), 260 deletions(-)
 create mode 100644 clang/include/clang/Sema/SemaBase.h
 create mode 100644 clang/lib/Sema/SemaBase.cpp

diff --git a/clang/include/clang/Sema/Sema.h b/clang/include/clang/Sema/Sema.h
index 8c98d8c7fef7a7..e97bb45bf04739 100644
--- a/clang/include/clang/Sema/Sema.h
+++ b/clang/include/clang/Sema/Sema.h
@@ -55,6 +55,7 @@
 #include "clang/Sema/ObjCMethodList.h"
 #include "clang/Sema/Ownership.h"
 #include "clang/Sema/Scope.h"
+#include "clang/Sema/SemaBase.h"
 #include "clang/Sema/SemaConcept.h"
 #include "clang/Sema/TypoCorrection.h"
 #include "clang/Sema/Weak.h"
@@ -422,7 +423,7 @@ enum class TemplateDeductionResult {
 
 /// Sema - This implements semantic analysis and AST building for C.
 /// \nosubgrouping
-class Sema final {
+class Sema final : public SemaBase {
   // Table of Contents
   // -
   // 1. Semantic Analysis (Sema.cpp)
@@ -512,195 +513,6 @@ class Sema final {
   ///
   void addExternalSource(ExternalSemaSource *E);
 
-  /// Helper class that creates diagnostics with optional
-  /// template instantiation stacks.
-  ///
-  /// This class provides a wrapper around the basic DiagnosticBuilder
-  /// class that emits diagnostics. ImmediateDiagBuilder is
-  /// responsible for emitting the diagnostic (as DiagnosticBuilder
-  /// does) and, if the diagnostic comes from inside a template
-  /// instantiation, printing the template instantiation stack as
-  /// well.
-  class ImmediateDiagBuilder : public DiagnosticBuilder {
-Sema &SemaRef;
-unsigned DiagID;
-
-  public:
-ImmediateDiagBuilder(DiagnosticBuilder &DB, Sema &SemaRef, unsigned DiagID)
-: DiagnosticBuilder(DB), SemaRef(SemaRef), DiagID(DiagID) {}
-ImmediateDiagBuilder(DiagnosticBuilder &&DB, Sema &SemaRef, unsigned 
DiagID)
-: DiagnosticBuilder(DB), SemaRef(SemaRef), DiagID(DiagID) {}
-
-// This is a cunning lie. DiagnosticBuilder actually performs move
-// construction in its copy constructor (but due to varied uses, it's not
-// possible to conveniently express this as actual move construction). So
-// the default copy ctor here is fine, because the base class disables the
-// source anyway, so the user-defined ~ImmediateDiagBuilder is a safe no-op
-// in that case anwyay.
-ImmediateDiagBuilder(const ImmediateDiagBuilder &) = default;
-
-~ImmediateDiagBuilder() {
-  // If we aren't active, there is nothing to do.
-  if (!isActive())
-return;
-
-  // Otherwise, we need to emit the diagnostic. First clear the diagnostic
-  // builder itself so it won't emit the diagnostic in its own destructor.
-  //
-  // This seems wasteful, in that as written the DiagnosticBuilder dtor 
will
-  // do its own needless checks to see if the diagnostic needs to be
-  // emitted. However, because we take care to ensure that the builder
-  // objects never escape, a sufficiently smart compiler will be able to
-  // eliminate that code.
-  Clear();
-
-  // Dispatch to Sema to emit the diagnostic.
-  SemaRef.EmitCurrentDiagnostic(DiagID);
-}
-
-/// Teach operator<< to produce an object of the correct type.
-template 
-friend const ImmediateDiagBuilder &
-operator<<(const ImmediateDiagBuilder &Diag, const T &Value) {
-  const DiagnosticBuilder &BaseDiag = Diag;
-  BaseDiag << Value;
-  return Diag;
-}
-
-// It is necessary to limit this to 

[clang] [clang][NFC] Introduce `SemaBase` (PR #87634)

2024-04-05 Thread via cfe-commits

https://github.com/cor3ntin approved this pull request.

Great, thanks!

https://github.com/llvm/llvm-project/pull/87634
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits