From ba0f991a78706068cc2e6a1c4ef4b0d8f7ce748b Mon Sep 17 00:00:00 2001 From: Juergen Ributzka Date: Tue, 19 Nov 2013 03:08:35 +0000 Subject: [weak vtables] Place class definitions into anonymous namespaces to prevent weak vtables. This patch places class definitions in implementation files into anonymous namespaces to prevent weak vtables. This eliminates the need of providing an out-of-line definition to pin the vtable explicitly to the file. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@195092 91177308-0d34-0410-b5e6-96231b3b80d8 --- examples/ExceptionDemo/ExceptionDemo.cpp | 8 +++----- examples/Kaleidoscope/Chapter2/toy.cpp | 17 +++-------------- examples/Kaleidoscope/Chapter3/toy.cpp | 8 +++----- examples/Kaleidoscope/Chapter4/toy.cpp | 8 +++----- examples/Kaleidoscope/Chapter5/toy.cpp | 8 +++----- examples/Kaleidoscope/Chapter6/toy.cpp | 8 +++----- examples/Kaleidoscope/Chapter7/toy.cpp | 8 +++----- 7 files changed, 21 insertions(+), 44 deletions(-) (limited to 'examples') diff --git a/examples/ExceptionDemo/ExceptionDemo.cpp b/examples/ExceptionDemo/ExceptionDemo.cpp index 6a0d0d027f..d6954e83be 100644 --- a/examples/ExceptionDemo/ExceptionDemo.cpp +++ b/examples/ExceptionDemo/ExceptionDemo.cpp @@ -1562,7 +1562,7 @@ llvm::Function *createUnwindExceptionTest(llvm::Module &module, return(outerCatchFunct); } - +namespace { /// Represents our foreign exceptions class OurCppRunException : public std::runtime_error { public: @@ -1577,11 +1577,9 @@ public: std::runtime_error::operator=(toCopy))); } - ~OurCppRunException (void) throw (); + virtual ~OurCppRunException (void) throw () {} }; - -// Provide out-of-line definition to prevent weak vtable. -OurCppRunException::~OurCppRunException() throw () {} +} // end anonymous namespace /// Throws foreign C++ exception. /// @param ignoreIt unused parameter that allows function to match implied diff --git a/examples/Kaleidoscope/Chapter2/toy.cpp b/examples/Kaleidoscope/Chapter2/toy.cpp index 99ec90e9b0..cd901394a5 100644 --- a/examples/Kaleidoscope/Chapter2/toy.cpp +++ b/examples/Kaleidoscope/Chapter2/toy.cpp @@ -75,18 +75,17 @@ static int gettok() { //===----------------------------------------------------------------------===// // Abstract Syntax Tree (aka Parse Tree) //===----------------------------------------------------------------------===// - +namespace { /// ExprAST - Base class for all expression nodes. class ExprAST { public: - virtual ~ExprAST(); + virtual ~ExprAST() {} }; /// NumberExprAST - Expression class for numeric literals like "1.0". class NumberExprAST : public ExprAST { public: NumberExprAST(double val) {} - virtual ~NumberExprAST(); }; /// VariableExprAST - Expression class for referencing a variable, like "a". @@ -94,14 +93,12 @@ class VariableExprAST : public ExprAST { std::string Name; public: VariableExprAST(const std::string &name) : Name(name) {} - virtual ~VariableExprAST(); }; /// BinaryExprAST - Expression class for a binary operator. class BinaryExprAST : public ExprAST { public: BinaryExprAST(char op, ExprAST *lhs, ExprAST *rhs) {} - virtual ~BinaryExprAST(); }; /// CallExprAST - Expression class for function calls. @@ -111,16 +108,8 @@ class CallExprAST : public ExprAST { public: CallExprAST(const std::string &callee, std::vector &args) : Callee(callee), Args(args) {} - virtual ~CallExprAST(); }; -// Provide out-of-line definitions to prevent weak vtables. -ExprAST::~ExprAST() {} -NumberExprAST::~NumberExprAST() {} -VariableExprAST::~VariableExprAST() {} -BinaryExprAST::~BinaryExprAST() {} -CallExprAST::~CallExprAST() {} - /// PrototypeAST - This class represents the "prototype" for a function, /// which captures its name, and its argument names (thus implicitly the number /// of arguments the function takes). @@ -138,6 +127,7 @@ class FunctionAST { public: FunctionAST(PrototypeAST *proto, ExprAST *body) {} }; +} // end anonymous namespace //===----------------------------------------------------------------------===// // Parser @@ -169,7 +159,6 @@ static int GetTokPrecedence() { /// Error* - These are little helper functions for error handling. ExprAST *Error(const char *Str) { fprintf(stderr, "Error: %s\n", Str);return 0;} PrototypeAST *ErrorP(const char *Str) { Error(Str); return 0; } -FunctionAST *ErrorF(const char *Str) { Error(Str); return 0; } static ExprAST *ParseExpression(); diff --git a/examples/Kaleidoscope/Chapter3/toy.cpp b/examples/Kaleidoscope/Chapter3/toy.cpp index 2494345c3f..a7b60ba322 100644 --- a/examples/Kaleidoscope/Chapter3/toy.cpp +++ b/examples/Kaleidoscope/Chapter3/toy.cpp @@ -80,17 +80,14 @@ static int gettok() { //===----------------------------------------------------------------------===// // Abstract Syntax Tree (aka Parse Tree) //===----------------------------------------------------------------------===// - +namespace { /// ExprAST - Base class for all expression nodes. class ExprAST { public: - virtual ~ExprAST(); + virtual ~ExprAST() {} virtual Value *Codegen() = 0; }; -// Provide out-of-line definition to prevent weak vtable. -ExprAST::~ExprAST() {} - /// NumberExprAST - Expression class for numeric literals like "1.0". class NumberExprAST : public ExprAST { double Val; @@ -150,6 +147,7 @@ public: Function *Codegen(); }; +} // end anonymous namespace //===----------------------------------------------------------------------===// // Parser diff --git a/examples/Kaleidoscope/Chapter4/toy.cpp b/examples/Kaleidoscope/Chapter4/toy.cpp index 4d56c0f2df..27c3f4f22d 100644 --- a/examples/Kaleidoscope/Chapter4/toy.cpp +++ b/examples/Kaleidoscope/Chapter4/toy.cpp @@ -87,17 +87,14 @@ static int gettok() { //===----------------------------------------------------------------------===// // Abstract Syntax Tree (aka Parse Tree) //===----------------------------------------------------------------------===// - +namespace { /// ExprAST - Base class for all expression nodes. class ExprAST { public: - virtual ~ExprAST(); + virtual ~ExprAST() {} virtual Value *Codegen() = 0; }; -// Provide out-of-line definition to prevent weak vtable. -ExprAST::~ExprAST() {} - /// NumberExprAST - Expression class for numeric literals like "1.0". class NumberExprAST : public ExprAST { double Val; @@ -157,6 +154,7 @@ public: Function *Codegen(); }; +} // end anonymous namespace //===----------------------------------------------------------------------===// // Parser diff --git a/examples/Kaleidoscope/Chapter5/toy.cpp b/examples/Kaleidoscope/Chapter5/toy.cpp index 113016a326..fe4cdfe757 100644 --- a/examples/Kaleidoscope/Chapter5/toy.cpp +++ b/examples/Kaleidoscope/Chapter5/toy.cpp @@ -96,17 +96,14 @@ static int gettok() { //===----------------------------------------------------------------------===// // Abstract Syntax Tree (aka Parse Tree) //===----------------------------------------------------------------------===// - +namespace { /// ExprAST - Base class for all expression nodes. class ExprAST { public: - virtual ~ExprAST(); + virtual ~ExprAST() {} virtual Value *Codegen() = 0; }; -// Provide out-of-line definition to prevent weak vtable. -ExprAST::~ExprAST() {} - /// NumberExprAST - Expression class for numeric literals like "1.0". class NumberExprAST : public ExprAST { double Val; @@ -186,6 +183,7 @@ public: Function *Codegen(); }; +} // end anonymous namespace //===----------------------------------------------------------------------===// // Parser diff --git a/examples/Kaleidoscope/Chapter6/toy.cpp b/examples/Kaleidoscope/Chapter6/toy.cpp index 70f233bf48..b7b347db0d 100644 --- a/examples/Kaleidoscope/Chapter6/toy.cpp +++ b/examples/Kaleidoscope/Chapter6/toy.cpp @@ -101,17 +101,14 @@ static int gettok() { //===----------------------------------------------------------------------===// // Abstract Syntax Tree (aka Parse Tree) //===----------------------------------------------------------------------===// - +namespace { /// ExprAST - Base class for all expression nodes. class ExprAST { public: - virtual ~ExprAST(); + virtual ~ExprAST() {} virtual Value *Codegen() = 0; }; -// Provide out-of-line definition to prevent weak vtable. -ExprAST::~ExprAST() {} - /// NumberExprAST - Expression class for numeric literals like "1.0". class NumberExprAST : public ExprAST { double Val; @@ -214,6 +211,7 @@ public: Function *Codegen(); }; +} // end anonymous namespace //===----------------------------------------------------------------------===// // Parser diff --git a/examples/Kaleidoscope/Chapter7/toy.cpp b/examples/Kaleidoscope/Chapter7/toy.cpp index a05d134a1a..5c66f5c837 100644 --- a/examples/Kaleidoscope/Chapter7/toy.cpp +++ b/examples/Kaleidoscope/Chapter7/toy.cpp @@ -105,17 +105,14 @@ static int gettok() { //===----------------------------------------------------------------------===// // Abstract Syntax Tree (aka Parse Tree) //===----------------------------------------------------------------------===// - +namespace { /// ExprAST - Base class for all expression nodes. class ExprAST { public: - virtual ~ExprAST(); + virtual ~ExprAST() {} virtual Value *Codegen() = 0; }; -// Provide out-of-line definition to prevent weak vtable. -ExprAST::~ExprAST() {} - /// NumberExprAST - Expression class for numeric literals like "1.0". class NumberExprAST : public ExprAST { double Val; @@ -232,6 +229,7 @@ public: Function *Codegen(); }; +} // end anonymous namespace //===----------------------------------------------------------------------===// // Parser -- cgit v1.2.3