From c426b71334dd7e9f15bcfe8082cabc616d2f4b4a Mon Sep 17 00:00:00 2001 From: Nikolai Kosjar Date: Mon, 13 May 2013 10:20:00 +0200 Subject: C++: Use soft asserts instead of assert() Some qmake versions ignore "DEFINES += NDEBUG" on Windows. Change-Id: Ibdf3b1036c2331dcef61dcd278463c02754044d2 Reviewed-by: Erik Verbruggen --- src/libs/3rdparty/cplusplus/AST.cpp | 5 ++- src/libs/3rdparty/cplusplus/AST.h | 4 +- src/libs/3rdparty/cplusplus/Bind.cpp | 63 ++++++++++++++++-------------- src/libs/3rdparty/cplusplus/Lexer.cpp | 16 ++++---- src/libs/3rdparty/cplusplus/MemoryPool.cpp | 6 ++- src/libs/3rdparty/cplusplus/Names.cpp | 3 +- src/libs/3rdparty/cplusplus/Scope.cpp | 7 +++- src/libs/3rdparty/cplusplus/Symbol.cpp | 6 ++- src/libs/3rdparty/cplusplus/Templates.cpp | 9 +++-- src/libs/3rdparty/cplusplus/cplusplus.pri | 1 + src/libs/3rdparty/cplusplus/cppassert.h | 46 ++++++++++++++++++++++ 11 files changed, 113 insertions(+), 53 deletions(-) create mode 100644 src/libs/3rdparty/cplusplus/cppassert.h diff --git a/src/libs/3rdparty/cplusplus/AST.cpp b/src/libs/3rdparty/cplusplus/AST.cpp index 13c4568fb7..da1f61a932 100644 --- a/src/libs/3rdparty/cplusplus/AST.cpp +++ b/src/libs/3rdparty/cplusplus/AST.cpp @@ -23,7 +23,8 @@ #include "ASTMatcher.h" #include "MemoryPool.h" -#include +#include "cppassert.h" + #include @@ -42,7 +43,7 @@ AST::AST() { } AST::~AST() -{ assert(0); } +{ CPP_CHECK(0); } void AST::accept(ASTVisitor *visitor) { diff --git a/src/libs/3rdparty/cplusplus/AST.h b/src/libs/3rdparty/cplusplus/AST.h index 9699ff8e0d..a2d3f81ba8 100644 --- a/src/libs/3rdparty/cplusplus/AST.h +++ b/src/libs/3rdparty/cplusplus/AST.h @@ -47,7 +47,7 @@ public: if (value) return value->firstToken(); - // ### assert(0); + // ### CPP_CHECK(0); return 0; } @@ -58,7 +58,7 @@ public: if (lv) return lv->lastToken(); - // ### assert(0); + // ### CPP_CHECK(0); return 0; } diff --git a/src/libs/3rdparty/cplusplus/Bind.cpp b/src/libs/3rdparty/cplusplus/Bind.cpp index 3f2ffea7c8..15ac4fa9ee 100644 --- a/src/libs/3rdparty/cplusplus/Bind.cpp +++ b/src/libs/3rdparty/cplusplus/Bind.cpp @@ -27,10 +27,13 @@ #include "CoreTypes.h" #include "Literals.h" #include "Scope.h" + +#include "cppassert.h" + #include #include #include -#include + using namespace CPlusPlus; @@ -285,7 +288,7 @@ void Bind::postVisit(AST *) bool Bind::visit(ObjCSelectorArgumentAST *ast) { (void) ast; - assert(!"unreachable"); + CPP_CHECK(!"unreachable"); return false; } @@ -303,7 +306,7 @@ const Name *Bind::objCSelectorArgument(ObjCSelectorArgumentAST *ast, bool *hasAr bool Bind::visit(AttributeAST *ast) { (void) ast; - assert(!"unreachable"); + CPP_CHECK(!"unreachable"); return false; } @@ -331,7 +334,7 @@ void Bind::attribute(AttributeAST *ast) bool Bind::visit(DeclaratorAST *ast) { (void) ast; - assert(!"unreachable"); + CPP_CHECK(!"unreachable"); return false; } @@ -380,14 +383,14 @@ FullySpecifiedType Bind::declarator(DeclaratorAST *ast, const FullySpecifiedType bool Bind::visit(QtPropertyDeclarationItemAST *ast) { (void) ast; - assert(!"unreachable"); + CPP_CHECK(!"unreachable"); return false; } bool Bind::visit(QtInterfaceNameAST *ast) { (void) ast; - assert(!"unreachable"); + CPP_CHECK(!"unreachable"); return false; } @@ -405,7 +408,7 @@ void Bind::qtInterfaceName(QtInterfaceNameAST *ast) bool Bind::visit(BaseSpecifierAST *ast) { (void) ast; - assert(!"unreachable"); + CPP_CHECK(!"unreachable"); return false; } @@ -433,7 +436,7 @@ void Bind::baseSpecifier(BaseSpecifierAST *ast, unsigned colon_token, Class *kla bool Bind::visit(CtorInitializerAST *ast) { (void) ast; - assert(!"unreachable"); + CPP_CHECK(!"unreachable"); return false; } @@ -452,7 +455,7 @@ void Bind::ctorInitializer(CtorInitializerAST *ast, Function *fun) bool Bind::visit(EnumeratorAST *ast) { (void) ast; - assert(!"unreachable"); + CPP_CHECK(!"unreachable"); return false; } @@ -482,7 +485,7 @@ void Bind::enumerator(EnumeratorAST *ast, Enum *symbol) bool Bind::visit(DynamicExceptionSpecificationAST *ast) { (void) ast; - assert(!"unreachable"); + CPP_CHECK(!"unreachable"); return false; } @@ -510,7 +513,7 @@ FullySpecifiedType Bind::exceptionSpecification(ExceptionSpecificationAST *ast, bool Bind::visit(MemInitializerAST *ast) { (void) ast; - assert(!"unreachable"); + CPP_CHECK(!"unreachable"); return false; } @@ -529,7 +532,7 @@ void Bind::memInitializer(MemInitializerAST *ast, Function *fun) bool Bind::visit(NestedNameSpecifierAST *ast) { (void) ast; - assert(!"unreachable"); + CPP_CHECK(!"unreachable"); return false; } @@ -568,7 +571,7 @@ void Bind::newPlacement(ExpressionListParenAST *ast) bool Bind::visit(NewArrayDeclaratorAST *ast) { (void) ast; - assert(!"unreachable"); + CPP_CHECK(!"unreachable"); return false; } @@ -588,7 +591,7 @@ FullySpecifiedType Bind::newArrayDeclarator(NewArrayDeclaratorAST *ast, const Fu bool Bind::visit(NewTypeIdAST *ast) { (void) ast; - assert(!"unreachable"); + CPP_CHECK(!"unreachable"); return false; } @@ -615,7 +618,7 @@ FullySpecifiedType Bind::newTypeId(NewTypeIdAST *ast) bool Bind::visit(OperatorAST *ast) { (void) ast; - assert(!"unreachable"); + CPP_CHECK(!"unreachable"); return false; } @@ -807,7 +810,7 @@ OperatorNameId::Kind Bind::cppOperator(OperatorAST *ast) bool Bind::visit(ParameterDeclarationClauseAST *ast) { (void) ast; - assert(!"unreachable"); + CPP_CHECK(!"unreachable"); return false; } @@ -836,7 +839,7 @@ void Bind::parameterDeclarationClause(ParameterDeclarationClauseAST *ast, unsign bool Bind::visit(TranslationUnitAST *ast) { (void) ast; - assert(!"unreachable"); + CPP_CHECK(!"unreachable"); return false; } @@ -853,7 +856,7 @@ void Bind::translationUnit(TranslationUnitAST *ast) bool Bind::visit(ObjCProtocolRefsAST *ast) { (void) ast; - assert(!"unreachable"); + CPP_CHECK(!"unreachable"); return false; } @@ -875,7 +878,7 @@ void Bind::objCProtocolRefs(ObjCProtocolRefsAST *ast, Symbol *objcClassOrProtoco bool Bind::visit(ObjCMessageArgumentAST *ast) { (void) ast; - assert(!"unreachable"); + CPP_CHECK(!"unreachable"); return false; } @@ -890,7 +893,7 @@ void Bind::objCMessageArgument(ObjCMessageArgumentAST *ast) bool Bind::visit(ObjCTypeNameAST *ast) { (void) ast; - assert(!"unreachable"); + CPP_CHECK(!"unreachable"); return false; } @@ -907,7 +910,7 @@ FullySpecifiedType Bind::objCTypeName(ObjCTypeNameAST *ast) bool Bind::visit(ObjCInstanceVariablesDeclarationAST *ast) { (void) ast; - assert(!"unreachable"); + CPP_CHECK(!"unreachable"); return false; } @@ -928,7 +931,7 @@ void Bind::objCInstanceVariablesDeclaration(ObjCInstanceVariablesDeclarationAST bool Bind::visit(ObjCPropertyAttributeAST *ast) { (void) ast; - assert(!"unreachable"); + CPP_CHECK(!"unreachable"); return false; } @@ -945,7 +948,7 @@ void Bind::objCPropertyAttribute(ObjCPropertyAttributeAST *ast) bool Bind::visit(ObjCMessageArgumentDeclarationAST *ast) { (void) ast; - assert(!"unreachable"); + CPP_CHECK(!"unreachable"); return false; } @@ -970,7 +973,7 @@ void Bind::objCMessageArgumentDeclaration(ObjCMessageArgumentDeclarationAST *ast bool Bind::visit(ObjCMethodPrototypeAST *ast) { (void) ast; - assert(!"unreachable"); + CPP_CHECK(!"unreachable"); return false; } @@ -1013,7 +1016,7 @@ ObjCMethod *Bind::objCMethodPrototype(ObjCMethodPrototypeAST *ast) bool Bind::visit(ObjCSynthesizedPropertyAST *ast) { (void) ast; - assert(!"unreachable"); + CPP_CHECK(!"unreachable"); return false; } @@ -1030,7 +1033,7 @@ void Bind::objCSynthesizedProperty(ObjCSynthesizedPropertyAST *ast) bool Bind::visit(LambdaIntroducerAST *ast) { (void) ast; - assert(!"unreachable"); + CPP_CHECK(!"unreachable"); return false; } @@ -1047,7 +1050,7 @@ void Bind::lambdaIntroducer(LambdaIntroducerAST *ast) bool Bind::visit(LambdaCaptureAST *ast) { (void) ast; - assert(!"unreachable"); + CPP_CHECK(!"unreachable"); return false; } @@ -1065,7 +1068,7 @@ void Bind::lambdaCapture(LambdaCaptureAST *ast) bool Bind::visit(CaptureAST *ast) { (void) ast; - assert(!"unreachable"); + CPP_CHECK(!"unreachable"); return false; } @@ -1080,7 +1083,7 @@ void Bind::capture(CaptureAST *ast) bool Bind::visit(LambdaDeclaratorAST *ast) { (void) ast; - assert(!"unreachable"); + CPP_CHECK(!"unreachable"); return false; } @@ -1111,7 +1114,7 @@ void Bind::lambdaDeclarator(LambdaDeclaratorAST *ast) bool Bind::visit(TrailingReturnTypeAST *ast) { (void) ast; - assert(!"unreachable"); + CPP_CHECK(!"unreachable"); return false; } diff --git a/src/libs/3rdparty/cplusplus/Lexer.cpp b/src/libs/3rdparty/cplusplus/Lexer.cpp index 18f68047b8..1c56558773 100644 --- a/src/libs/3rdparty/cplusplus/Lexer.cpp +++ b/src/libs/3rdparty/cplusplus/Lexer.cpp @@ -22,8 +22,10 @@ #include "Control.h" #include "TranslationUnit.h" #include "Literals.h" + +#include "cppassert.h" + #include -#include using namespace CPlusPlus; @@ -210,7 +212,7 @@ void Lexer::scan_helper(Token *tok) case '\\': while (_yychar != '\n' && std::isspace(_yychar)) yyinp(); - // ### assert(! _yychar || _yychar == '\n'); + // ### CPP_CHECK(! _yychar || _yychar == '\n'); if (_yychar == '\n') { tok->f.joined = true; tok->f.newline = false; @@ -278,7 +280,7 @@ void Lexer::scan_helper(Token *tok) tok->f.kind = T_DOT_STAR; } else if (_yychar == '.') { yyinp(); - // ### assert(_yychar); + // ### CPP_CHECK(_yychar); if (_yychar == '.') { yyinp(); tok->f.kind = T_DOT_DOT_DOT; @@ -292,7 +294,7 @@ void Lexer::scan_helper(Token *tok) yyinp(); if (_yychar == '-' || _yychar == '+') { yyinp(); - // ### assert(std::isdigit(_yychar)); + // ### CPP_CHECK(std::isdigit(_yychar)); } } else if (std::isalnum(_yychar) || _yychar == '.') { yyinp(); @@ -497,7 +499,7 @@ void Lexer::scan_helper(Token *tok) while (_yychar && _yychar != '>') yyinp(); int yylen = _currentChar - yytext; - // ### assert(_yychar == '>'); + // ### CPP_CHECK(_yychar == '>'); if (_yychar == '>') yyinp(); if (control()) @@ -709,7 +711,7 @@ void Lexer::scanCharLiteral(Token *tok, unsigned char hint) void Lexer::scanUntilQuote(Token *tok, unsigned char quote) { - assert(quote == '"' || quote == '\''); + CPP_CHECK(quote == '"' || quote == '\''); const char *yytext = _currentChar; while (_yychar @@ -740,7 +742,7 @@ void Lexer::scanNumericLiteral(Token *tok) yyinp(); if (_yychar == '-' || _yychar == '+') { yyinp(); - // ### assert(std::isdigit(_yychar)); + // ### CPP_CHECK(std::isdigit(_yychar)); } } else if (std::isalnum(_yychar) || _yychar == '.') { yyinp(); diff --git a/src/libs/3rdparty/cplusplus/MemoryPool.cpp b/src/libs/3rdparty/cplusplus/MemoryPool.cpp index cab486cab2..5c1041d16c 100644 --- a/src/libs/3rdparty/cplusplus/MemoryPool.cpp +++ b/src/libs/3rdparty/cplusplus/MemoryPool.cpp @@ -19,8 +19,10 @@ // THE SOFTWARE. #include "MemoryPool.h" + +#include "cppassert.h" + #include -#include using namespace CPlusPlus; @@ -52,7 +54,7 @@ void MemoryPool::reset() void *MemoryPool::allocate_helper(size_t size) { - assert(size < BLOCK_SIZE); + CPP_CHECK(size < BLOCK_SIZE); if (++_blockCount == _allocatedBlocks) { if (! _allocatedBlocks) diff --git a/src/libs/3rdparty/cplusplus/Names.cpp b/src/libs/3rdparty/cplusplus/Names.cpp index 4a65fea2a8..4d92110284 100644 --- a/src/libs/3rdparty/cplusplus/Names.cpp +++ b/src/libs/3rdparty/cplusplus/Names.cpp @@ -21,9 +21,8 @@ #include "Names.h" #include "NameVisitor.h" #include "Literals.h" -#include -#include #include +#include using namespace CPlusPlus; diff --git a/src/libs/3rdparty/cplusplus/Scope.cpp b/src/libs/3rdparty/cplusplus/Scope.cpp index 5dbc4ae183..66d422ad35 100644 --- a/src/libs/3rdparty/cplusplus/Scope.cpp +++ b/src/libs/3rdparty/cplusplus/Scope.cpp @@ -23,7 +23,9 @@ #include "Names.h" #include "Literals.h" #include "Templates.h" -#include + +#include "cppassert.h" + #include using namespace CPlusPlus; @@ -107,6 +109,8 @@ SymbolTable::~SymbolTable() void SymbolTable::enterSymbol(Symbol *symbol) { + CPP_ASSERT(! symbol->_scope || symbol->enclosingScope() == _owner, return); + if (++_symbolCount == _allocatedSymbols) { _allocatedSymbols <<= 1; if (! _allocatedSymbols) @@ -115,7 +119,6 @@ void SymbolTable::enterSymbol(Symbol *symbol) _symbols = reinterpret_cast(realloc(_symbols, sizeof(Symbol *) * _allocatedSymbols)); } - assert(! symbol->_scope || symbol->enclosingScope() == _owner); symbol->_index = _symbolCount; symbol->_scope = _owner; _symbols[_symbolCount] = symbol; diff --git a/src/libs/3rdparty/cplusplus/Symbol.cpp b/src/libs/3rdparty/cplusplus/Symbol.cpp index 0510117f8b..f5506b8c89 100644 --- a/src/libs/3rdparty/cplusplus/Symbol.cpp +++ b/src/libs/3rdparty/cplusplus/Symbol.cpp @@ -29,7 +29,9 @@ #include "NameVisitor.h" #include "Scope.h" #include "Templates.h" -#include + +#include "cppassert.h" + using namespace CPlusPlus; @@ -233,7 +235,7 @@ Scope *Symbol::enclosingScope() const void Symbol::setScope(Scope *scope) { - assert(! _scope); + CPP_CHECK(! _scope); _scope = scope; } diff --git a/src/libs/3rdparty/cplusplus/Templates.cpp b/src/libs/3rdparty/cplusplus/Templates.cpp index 5edef37c7f..d3bbf06583 100644 --- a/src/libs/3rdparty/cplusplus/Templates.cpp +++ b/src/libs/3rdparty/cplusplus/Templates.cpp @@ -25,7 +25,8 @@ #include "Names.h" #include "Literals.h" -#include +#include "cppassert.h" + using namespace CPlusPlus; @@ -184,7 +185,7 @@ Symbol *CloneSymbol::cloneSymbol(Symbol *symbol, Subst *subst) accept(symbol); std::swap(_symbol, r); std::swap(_subst, subst); - assert(r != 0); + CPP_CHECK(r != 0); return r; } @@ -399,7 +400,7 @@ const Name *CloneName::cloneName(const Name *name, Subst *subst) accept(name); std::swap(_name, r); std::swap(_subst, subst); - assert(r != 0); + CPP_CHECK(r != 0); return r; } @@ -448,7 +449,7 @@ void CloneName::visit(const QualifiedNameId *name) void CloneName::visit(const SelectorNameId *name) { - assert(name->nameCount() > 0); + CPP_CHECK(name->nameCount() > 0); std::vector names(name->nameCount()); for (unsigned i = 0; i < names.size(); ++i) names[i] = _clone->name(name->nameAt(i), _subst); diff --git a/src/libs/3rdparty/cplusplus/cplusplus.pri b/src/libs/3rdparty/cplusplus/cplusplus.pri index 62c14f3829..e179f54c89 100644 --- a/src/libs/3rdparty/cplusplus/cplusplus.pri +++ b/src/libs/3rdparty/cplusplus/cplusplus.pri @@ -3,6 +3,7 @@ DEPENDPATH += $$PWD INCLUDEPATH += $$PWD HEADERS += \ + $$PWD/cppassert.h \ $$PWD/CPlusPlus.h \ $$PWD/AST.h \ $$PWD/ASTVisitor.h \ diff --git a/src/libs/3rdparty/cplusplus/cppassert.h b/src/libs/3rdparty/cplusplus/cppassert.h new file mode 100644 index 0000000000..22c5cae472 --- /dev/null +++ b/src/libs/3rdparty/cplusplus/cppassert.h @@ -0,0 +1,46 @@ +/**************************************************************************** + * ** + * ** Copyright (C) 2013 Digia Plc and/or its subsidiary(-ies). + * ** Contact: http://www.qt-project.org/legal + * ** + * ** This file is part of Qt Creator. + * ** + * ** Commercial License Usage + * ** Licensees holding valid commercial Qt licenses may use this file in + * ** accordance with the commercial license agreement provided with the + * ** Software or, alternatively, in accordance with the terms contained in + * ** a written agreement between you and Digia. For licensing terms and + * ** conditions see http://qt.digia.com/licensing. For further information + * ** use the contact form at http://qt.digia.com/contact-us. + * ** + * ** GNU Lesser General Public License Usage + * ** Alternatively, this file may be used under the terms of the GNU Lesser + * ** General Public License version 2.1 as published by the Free Software + * ** Foundation and appearing in the file LICENSE.LGPL included in the + * ** packaging of this file. Please review the following information to + * ** ensure the GNU Lesser General Public License version 2.1 requirements + * ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. + * ** + * ** In addition, as a special exception, Digia gives you certain additional + * ** rights. These rights are described in the Digia Qt LGPL Exception + * ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package. + * ** + * ****************************************************************************/ + +#ifndef SOFT_ASSERT_H +#define SOFT_ASSERT_H + +#include + +#define CPP_ASSERT_STRINGIFY_HELPER(x) #x +#define CPP_ASSERT_STRINGIFY(x) CPP_ASSERT_STRINGIFY_HELPER(x) +#define CPP_ASSERT_STRING(cond) std::cerr \ + << "SOFT ASSERT: \"" cond"\" in file " __FILE__ ", line " CPP_ASSERT_STRINGIFY(__LINE__) \ + << std::endl; + +#define CPP_ASSERT(cond, action) if (cond) {} else { CPP_ASSERT_STRING(#cond); action; } do {} while (0) +#define CPP_CHECK(cond) if (cond) {} else { CPP_ASSERT_STRING(#cond); } do {} while (0) + +#endif // SOFT_ASSERT_H + + -- cgit v1.2.3