Skip to content

Add macro to suppress -Wunnecessary-virtual-specifier #139614

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions clang/include/clang/AST/Decl.h
Original file line number Diff line number Diff line change
Expand Up @@ -172,7 +172,7 @@ class PragmaCommentDecl final
PragmaMSCommentKind CommentKind)
: Decl(PragmaComment, TU, CommentLoc), CommentKind(CommentKind) {}

virtual void anchor();
LLVM_DECLARE_VIRTUAL_ANCHOR_FUNCTION();

public:
static PragmaCommentDecl *Create(const ASTContext &C, TranslationUnitDecl *DC,
Expand Down Expand Up @@ -206,7 +206,7 @@ class PragmaDetectMismatchDecl final
size_t ValueStart)
: Decl(PragmaDetectMismatch, TU, Loc), ValueStart(ValueStart) {}

virtual void anchor();
LLVM_DECLARE_VIRTUAL_ANCHOR_FUNCTION();

public:
static PragmaDetectMismatchDecl *Create(const ASTContext &C,
Expand Down Expand Up @@ -5031,7 +5031,7 @@ class ImportDecl final : public Decl,
/// export void foo();
/// \endcode
class ExportDecl final : public Decl, public DeclContext {
virtual void anchor();
LLVM_DECLARE_VIRTUAL_ANCHOR_FUNCTION();

private:
friend class ASTDeclReader;
Expand Down
2 changes: 1 addition & 1 deletion clang/include/clang/AST/DeclCXX.h
Original file line number Diff line number Diff line change
Expand Up @@ -3296,7 +3296,7 @@ class LifetimeExtendedTemporaryDecl final

mutable APValue *Value = nullptr;

virtual void anchor();
LLVM_DECLARE_VIRTUAL_ANCHOR_FUNCTION();

LifetimeExtendedTemporaryDecl(Expr *Temp, ValueDecl *EDecl, unsigned Mangling)
: Decl(Decl::LifetimeExtendedTemporary, EDecl->getDeclContext(),
Expand Down
2 changes: 1 addition & 1 deletion clang/include/clang/AST/DeclFriend.h
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ class ASTContext;
class FriendDecl final
: public Decl,
private llvm::TrailingObjects<FriendDecl, TemplateParameterList *> {
virtual void anchor();
LLVM_DECLARE_VIRTUAL_ANCHOR_FUNCTION();

public:
using FriendUnion = llvm::PointerUnion<NamedDecl *, TypeSourceInfo *>;
Expand Down
6 changes: 3 additions & 3 deletions clang/include/clang/AST/DeclOpenMP.h
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ template <typename U> class OMPDeclarativeDirective : public U {
class OMPThreadPrivateDecl final : public OMPDeclarativeDirective<Decl> {
friend class OMPDeclarativeDirective<Decl>;

virtual void anchor();
LLVM_DECLARE_VIRTUAL_ANCHOR_FUNCTION();

OMPThreadPrivateDecl(DeclContext *DC = nullptr,
SourceLocation L = SourceLocation())
Expand Down Expand Up @@ -418,7 +418,7 @@ class OMPRequiresDecl final : public OMPDeclarativeDirective<Decl> {
friend class OMPDeclarativeDirective<Decl>;
friend class ASTDeclReader;

virtual void anchor();
LLVM_DECLARE_VIRTUAL_ANCHOR_FUNCTION();

OMPRequiresDecl(DeclContext *DC, SourceLocation L)
: OMPDeclarativeDirective<Decl>(OMPRequires, DC, L) {}
Expand Down Expand Up @@ -475,7 +475,7 @@ class OMPAllocateDecl final : public OMPDeclarativeDirective<Decl> {
friend class OMPDeclarativeDirective<Decl>;
friend class ASTDeclReader;

virtual void anchor();
LLVM_DECLARE_VIRTUAL_ANCHOR_FUNCTION();

OMPAllocateDecl(DeclContext *DC, SourceLocation L)
: OMPDeclarativeDirective<Decl>(OMPAllocate, DC, L) {}
Expand Down
2 changes: 1 addition & 1 deletion clang/include/clang/Driver/Action.h
Original file line number Diff line number Diff line change
Expand Up @@ -267,7 +267,7 @@ class BindArchAction : public Action {
/// programming model implementation needs and propagates the offloading kind to
/// its dependences.
class OffloadAction final : public Action {
virtual void anchor();
LLVM_DECLARE_VIRTUAL_ANCHOR_FUNCTION();

public:
/// Type used to communicate device actions. It associates bound architecture,
Expand Down
2 changes: 1 addition & 1 deletion clang/include/clang/Sema/Sema.h
Original file line number Diff line number Diff line change
Expand Up @@ -895,7 +895,7 @@ class Sema final : public SemaBase {
/// with a vtable when the vtable is emitted. Sema is final and not
/// polymorphic, but the debug info size savings are so significant that it is
/// worth adding a vtable just to take advantage of this optimization.
virtual void anchor();
LLVM_DECLARE_VIRTUAL_ANCHOR_FUNCTION();

const LangOptions &getLangOpts() const { return LangOpts; }
OpenCLOptions &getOpenCLOptions() { return OpenCLFeatures; }
Expand Down
2 changes: 1 addition & 1 deletion clang/lib/AST/ByteCode/InterpFrame.h
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@ class InterpFrame final : public Frame {
CodePtr getRetPC() const { return RetPC; }

/// Map a location to a source.
virtual SourceInfo getSource(CodePtr PC) const;
SourceInfo getSource(CodePtr PC) const;
const Expr *getExpr(CodePtr PC) const;
SourceLocation getLocation(CodePtr PC) const;
SourceRange getRange(CodePtr PC) const;
Expand Down
2 changes: 1 addition & 1 deletion clang/lib/CodeGen/CGOpenMPRuntimeGPU.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -323,7 +323,7 @@ class CheckVarsEscapingDeclContext final
CheckVarsEscapingDeclContext(CodeGenFunction &CGF,
ArrayRef<const ValueDecl *> TeamsReductions)
: CGF(CGF), EscapedDecls(llvm::from_range, TeamsReductions) {}
virtual ~CheckVarsEscapingDeclContext() = default;
~CheckVarsEscapingDeclContext() = default;
void VisitDeclStmt(const DeclStmt *S) {
if (!S)
return;
Expand Down
2 changes: 1 addition & 1 deletion clang/lib/CodeGen/CGStmtOpenMP.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4783,7 +4783,7 @@ class CheckVarsEscapingUntiedTaskDeclContext final

public:
explicit CheckVarsEscapingUntiedTaskDeclContext() = default;
virtual ~CheckVarsEscapingUntiedTaskDeclContext() = default;
~CheckVarsEscapingUntiedTaskDeclContext() = default;
void VisitDeclStmt(const DeclStmt *S) {
if (!S)
return;
Expand Down
4 changes: 2 additions & 2 deletions clang/lib/Driver/ToolChains/Hexagon.h
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,8 @@ class LLVM_LIBRARY_VISIBILITY Linker final : public Tool {
bool hasIntegratedCPP() const override { return false; }
bool isLinkJob() const override { return true; }

virtual void RenderExtraToolArgs(const JobAction &JA,
llvm::opt::ArgStringList &CmdArgs) const;
void RenderExtraToolArgs(const JobAction &JA,
llvm::opt::ArgStringList &CmdArgs) const;
void ConstructJob(Compilation &C, const JobAction &JA,
const InputInfo &Output, const InputInfoList &Inputs,
const llvm::opt::ArgList &TCArgs,
Expand Down
5 changes: 0 additions & 5 deletions llvm/cmake/modules/HandleLLVMOptions.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -882,11 +882,6 @@ if (LLVM_ENABLE_WARNINGS AND (LLVM_COMPILER_IS_GCC_COMPATIBLE OR CLANG_CL))
# The LLVM libraries have no stable C++ API, so -Wnoexcept-type is not useful.
append("-Wno-noexcept-type" CMAKE_CXX_FLAGS)

# LLVM has a policy of including virtual "anchor" functions to control
# where the vtable is emitted. In `final` classes, these are exactly what
# this warning detects: unnecessary virtual methods.
add_flag_if_supported("-Wno-unnecessary-virtual-specifier" CXX_SUPPORTS_UNNECESSARY_VIRTUAL_FLAG)

if (CMAKE_CXX_COMPILER_ID MATCHES "Clang")
append("-Wnon-virtual-dtor" CMAKE_CXX_FLAGS)
endif()
Expand Down
2 changes: 1 addition & 1 deletion llvm/include/llvm/Analysis/InstSimplifyFolder.h
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ class InstSimplifyFolder final : public IRBuilderFolder {
TargetFolder ConstFolder;
SimplifyQuery SQ;

virtual void anchor();
LLVM_DECLARE_VIRTUAL_ANCHOR_FUNCTION();

public:
explicit InstSimplifyFolder(const DataLayout &DL) : ConstFolder(DL), SQ(DL) {}
Expand Down
2 changes: 1 addition & 1 deletion llvm/include/llvm/Analysis/TargetFolder.h
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ class TargetFolder final : public IRBuilderFolder {
return ConstantFoldConstant(C, DL);
}

virtual void anchor();
LLVM_DECLARE_VIRTUAL_ANCHOR_FUNCTION();

public:
explicit TargetFolder(const DataLayout &DL) : DL(DL) {}
Expand Down
2 changes: 1 addition & 1 deletion llvm/include/llvm/IR/ConstantFolder.h
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ namespace llvm {

/// ConstantFolder - Create constants with minimum, target independent, folding.
class ConstantFolder final : public IRBuilderFolder {
virtual void anchor();
LLVM_DECLARE_VIRTUAL_ANCHOR_FUNCTION();

public:
explicit ConstantFolder() = default;
Expand Down
2 changes: 1 addition & 1 deletion llvm/include/llvm/IR/NoFolder.h
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ namespace llvm {

/// NoFolder - Create "constants" (actually, instructions) with no folding.
class NoFolder final : public IRBuilderFolder {
virtual void anchor();
LLVM_DECLARE_VIRTUAL_ANCHOR_FUNCTION();

public:
explicit NoFolder() = default;
Expand Down
18 changes: 18 additions & 0 deletions llvm/include/llvm/Support/Compiler.h
Original file line number Diff line number Diff line change
Expand Up @@ -710,4 +710,22 @@ void AnnotateIgnoreWritesEnd(const char *file, int line);
#define LLVM_PREFERRED_TYPE(T)
#endif

/// \macro LLVM_VIRTUAL_ANCHOR_FUNCTION
/// This macro is used to adhere to LLVM's policy that each class with a vtable
/// must have at least one out-of-line virtual function. This macro allows us
/// to declare such a function in `final` classes without triggering a warning.
// clang-format off
// Autoformatting makes this look awful.
#if defined(__clang__)
#define LLVM_DECLARE_VIRTUAL_ANCHOR_FUNCTION() \
_Pragma("clang diagnostic push") \
_Pragma("clang diagnostic ignored \"-Wunnecessary-virtual-specifier\"") \
virtual void anchor() \
_Pragma("clang diagnostic pop")
#else
#define LLVM_DECLARE_VIRTUAL_ANCHOR_FUNCTION() \
virtual void anchor()
#endif
// clang-format on

#endif
2 changes: 1 addition & 1 deletion llvm/include/llvm/Transforms/Scalar/GVNExpression.h
Original file line number Diff line number Diff line change
Expand Up @@ -452,7 +452,7 @@ class AggregateValueExpression final : public BasicExpression {
IntOperands[NumIntOperands++] = IntOperand;
}

virtual void allocateIntOperands(BumpPtrAllocator &Allocator) {
void allocateIntOperands(BumpPtrAllocator &Allocator) {
assert(!IntOperands && "Operands already allocated");
IntOperands = Allocator.Allocate<unsigned>(MaxIntOperands);
}
Expand Down
2 changes: 1 addition & 1 deletion llvm/lib/Target/AMDGPU/AMDGPULowerBufferFatPointers.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1212,7 +1212,7 @@ class FatPtrConstMaterializer final : public ValueMaterializer {
ValueToValueMapTy &UnderlyingMap)
: TypeMap(TypeMap),
InternalMapper(UnderlyingMap, RF_None, TypeMap, this) {}
virtual ~FatPtrConstMaterializer() = default;
~FatPtrConstMaterializer() = default;

Value *materialize(Value *V) override;
};
Expand Down
2 changes: 1 addition & 1 deletion llvm/lib/Target/X86/X86InstrInfo.h
Original file line number Diff line number Diff line change
Expand Up @@ -225,7 +225,7 @@ class X86InstrInfo final : public X86GenInstrInfo {
X86Subtarget &Subtarget;
const X86RegisterInfo RI;

virtual void anchor();
LLVM_DECLARE_VIRTUAL_ANCHOR_FUNCTION();

bool analyzeBranchImpl(MachineBasicBlock &MBB, MachineBasicBlock *&TBB,
MachineBasicBlock *&FBB,
Expand Down