From bbc00888dd79e66410dac27885cb4405d51996be Mon Sep 17 00:00:00 2001 From: David Blaikie Date: Wed, 13 Apr 2016 18:36:19 +0000 Subject: [PATCH] libclang: Use early-return to reduce indentation. & since I'll get blamed for all the lines anyway, remove some else-after-return and otherwise tidy things up. llvm-svn: 266224 --- clang/tools/libclang/CIndex.cpp | 200 +++++++++++++++++++--------------------- 1 file changed, 96 insertions(+), 104 deletions(-) diff --git a/clang/tools/libclang/CIndex.cpp b/clang/tools/libclang/CIndex.cpp index 3ee2c14..5add4a8 100644 --- a/clang/tools/libclang/CIndex.cpp +++ b/clang/tools/libclang/CIndex.cpp @@ -3459,146 +3459,138 @@ const char* clang_EvalResult_getAsStr(CXEvalResult E) { static const ExprEvalResult* evaluateExpr(Expr *expr, CXCursor C) { Expr::EvalResult ER; ASTContext &ctx = getCursorContext(C); - if (!expr) { + if (!expr) return nullptr; - } + expr = expr->IgnoreParens(); - bool res = expr->EvaluateAsRValue(ER, ctx); + if (!expr->EvaluateAsRValue(ER, ctx)) + return nullptr; + QualType rettype; CallExpr *callExpr; auto result = llvm::make_unique(); result->EvalType = CXEval_UnExposed; - if (res) { - - if (ER.Val.isInt()) { - result->EvalType = CXEval_Int; - result->EvalData.intVal = ER.Val.getInt().getExtValue(); - return result.release(); - } else if (ER.Val.isFloat()) { - - llvm::SmallVector Buffer; - ER.Val.getFloat().toString(Buffer); - std::string floatStr(Buffer.data(), Buffer.size()); - result->EvalType = CXEval_Float; - bool ignored; - llvm::APFloat apFloat = ER.Val.getFloat(); - apFloat.convert(llvm::APFloat::IEEEdouble, - llvm::APFloat::rmNearestTiesToEven, &ignored); - result->EvalData.floatVal = apFloat.convertToDouble(); - return result.release(); - - } else if (expr->getStmtClass() == Stmt::ImplicitCastExprClass) { - - const ImplicitCastExpr *I = dyn_cast(expr); - auto *subExpr = I->getSubExprAsWritten(); - if (subExpr->getStmtClass() == Stmt::StringLiteralClass || - subExpr->getStmtClass() == Stmt::ObjCStringLiteralClass) { - - const StringLiteral *StrE = nullptr; - const ObjCStringLiteral *ObjCExpr; - ObjCExpr = dyn_cast(subExpr); - - if (ObjCExpr) { - StrE = ObjCExpr->getString(); - result->EvalType = CXEval_ObjCStrLiteral; - } else { - StrE = cast(I->getSubExprAsWritten()); - result->EvalType = CXEval_StrLiteral; - } - - std::string strRef(StrE->getString().str()); - result->EvalData.stringVal = new char[strRef.size() + 1]; - strncpy((char*)result->EvalData.stringVal, strRef.c_str(), - strRef.size()); - result->EvalData.stringVal[strRef.size()] = '\0'; - return result.release(); - } + if (ER.Val.isInt()) { + result->EvalType = CXEval_Int; + result->EvalData.intVal = ER.Val.getInt().getExtValue(); + return result.release(); + } - } else if (expr->getStmtClass() == Stmt::ObjCStringLiteralClass || - expr->getStmtClass() == Stmt::StringLiteralClass) { + if (ER.Val.isFloat()) { + llvm::SmallVector Buffer; + ER.Val.getFloat().toString(Buffer); + std::string floatStr(Buffer.data(), Buffer.size()); + result->EvalType = CXEval_Float; + bool ignored; + llvm::APFloat apFloat = ER.Val.getFloat(); + apFloat.convert(llvm::APFloat::IEEEdouble, + llvm::APFloat::rmNearestTiesToEven, &ignored); + result->EvalData.floatVal = apFloat.convertToDouble(); + return result.release(); + } + if (expr->getStmtClass() == Stmt::ImplicitCastExprClass) { + const ImplicitCastExpr *I = dyn_cast(expr); + auto *subExpr = I->getSubExprAsWritten(); + if (subExpr->getStmtClass() == Stmt::StringLiteralClass || + subExpr->getStmtClass() == Stmt::ObjCStringLiteralClass) { const StringLiteral *StrE = nullptr; const ObjCStringLiteral *ObjCExpr; - ObjCExpr = dyn_cast(expr); + ObjCExpr = dyn_cast(subExpr); if (ObjCExpr) { StrE = ObjCExpr->getString(); result->EvalType = CXEval_ObjCStrLiteral; } else { - StrE = cast(expr); + StrE = cast(I->getSubExprAsWritten()); result->EvalType = CXEval_StrLiteral; } std::string strRef(StrE->getString().str()); result->EvalData.stringVal = new char[strRef.size() + 1]; - strncpy((char*)result->EvalData.stringVal, strRef.c_str(), - strRef.size()); + strncpy((char *)result->EvalData.stringVal, strRef.c_str(), + strRef.size()); result->EvalData.stringVal[strRef.size()] = '\0'; return result.release(); + } + } else if (expr->getStmtClass() == Stmt::ObjCStringLiteralClass || + expr->getStmtClass() == Stmt::StringLiteralClass) { + const StringLiteral *StrE = nullptr; + const ObjCStringLiteral *ObjCExpr; + ObjCExpr = dyn_cast(expr); - } else if (expr->getStmtClass() == Stmt::CStyleCastExprClass) { + if (ObjCExpr) { + StrE = ObjCExpr->getString(); + result->EvalType = CXEval_ObjCStrLiteral; + } else { + StrE = cast(expr); + result->EvalType = CXEval_StrLiteral; + } - CStyleCastExpr *CC = static_cast(expr); + std::string strRef(StrE->getString().str()); + result->EvalData.stringVal = new char[strRef.size() + 1]; + strncpy((char *)result->EvalData.stringVal, strRef.c_str(), strRef.size()); + result->EvalData.stringVal[strRef.size()] = '\0'; + return result.release(); + } - rettype = CC->getType(); - if (rettype.getAsString() == "CFStringRef" && - CC->getSubExpr()->getStmtClass() == Stmt::CallExprClass) { + if (expr->getStmtClass() == Stmt::CStyleCastExprClass) { + CStyleCastExpr *CC = static_cast(expr); - callExpr = static_cast(CC->getSubExpr()); - StringLiteral* S = getCFSTR_value(callExpr); - if (S) { - std::string strLiteral(S->getString().str()); - result->EvalType = CXEval_CFStr; + rettype = CC->getType(); + if (rettype.getAsString() == "CFStringRef" && + CC->getSubExpr()->getStmtClass() == Stmt::CallExprClass) { - result->EvalData.stringVal = new char[strLiteral.size() + 1]; - strncpy((char*)result->EvalData.stringVal, strLiteral.c_str(), - strLiteral.size()); - result->EvalData.stringVal[strLiteral.size()] = '\0'; - return result.release(); - } + callExpr = static_cast(CC->getSubExpr()); + StringLiteral *S = getCFSTR_value(callExpr); + if (S) { + std::string strLiteral(S->getString().str()); + result->EvalType = CXEval_CFStr; + + result->EvalData.stringVal = new char[strLiteral.size() + 1]; + strncpy((char *)result->EvalData.stringVal, strLiteral.c_str(), + strLiteral.size()); + result->EvalData.stringVal[strLiteral.size()] = '\0'; + return result.release(); } + } - } else if (expr->getStmtClass() == Stmt::CallExprClass) { + } else if (expr->getStmtClass() == Stmt::CallExprClass) { + callExpr = static_cast(expr); + rettype = callExpr->getCallReturnType(ctx); - callExpr = static_cast(expr); - rettype = callExpr->getCallReturnType(ctx); + if (rettype->isVectorType() || callExpr->getNumArgs() > 1) + return nullptr; - if (rettype->isVectorType() || callExpr->getNumArgs() > 1) + if (rettype->isIntegralType(ctx) || rettype->isRealFloatingType()) { + if (callExpr->getNumArgs() == 1 && + !callExpr->getArg(0)->getType()->isIntegralType(ctx)) return nullptr; - - if (rettype->isIntegralType(ctx) || rettype->isRealFloatingType()) { - if (callExpr->getNumArgs() == 1 && - !callExpr->getArg(0)->getType()->isIntegralType(ctx)) - return nullptr; - } else if(rettype.getAsString() == "CFStringRef") { - - StringLiteral* S = getCFSTR_value(callExpr); - if (S) { - std::string strLiteral(S->getString().str()); - result->EvalType = CXEval_CFStr; - result->EvalData.stringVal = new char[strLiteral.size() + 1]; - strncpy((char*)result->EvalData.stringVal, strLiteral.c_str(), - strLiteral.size()); - result->EvalData.stringVal[strLiteral.size()] = '\0'; - return result.release(); - } - } - - } else if (expr->getStmtClass() == Stmt::DeclRefExprClass) { - - DeclRefExpr *D = static_cast(expr); - ValueDecl *V = D->getDecl(); - if (V->getKind() == Decl::Function) { - std::string strName = V->getNameAsString(); - result->EvalType = CXEval_Other; - result->EvalData.stringVal = new char[strName.size() + 1]; - strncpy(result->EvalData.stringVal, strName.c_str(), strName.size()); - result->EvalData.stringVal[strName.size()] = '\0'; + } else if (rettype.getAsString() == "CFStringRef") { + + StringLiteral *S = getCFSTR_value(callExpr); + if (S) { + std::string strLiteral(S->getString().str()); + result->EvalType = CXEval_CFStr; + result->EvalData.stringVal = new char[strLiteral.size() + 1]; + strncpy((char *)result->EvalData.stringVal, strLiteral.c_str(), + strLiteral.size()); + result->EvalData.stringVal[strLiteral.size()] = '\0'; return result.release(); } } - + } else if (expr->getStmtClass() == Stmt::DeclRefExprClass) { + DeclRefExpr *D = static_cast(expr); + ValueDecl *V = D->getDecl(); + if (V->getKind() == Decl::Function) { + std::string strName = V->getNameAsString(); + result->EvalType = CXEval_Other; + result->EvalData.stringVal = new char[strName.size() + 1]; + strncpy(result->EvalData.stringVal, strName.c_str(), strName.size()); + result->EvalData.stringVal[strName.size()] = '\0'; + return result.release(); + } } return nullptr; -- 2.7.4