[PATCH] D49051: [libclang] check that the cursor is declaration before trying to evaluate the cursor like a declaration
Alex Lorenz via Phabricator via cfe-commits
cfe-commits at lists.llvm.org
Mon Jul 9 11:41:09 PDT 2018
arphaman marked an inline comment as done.
arphaman added inline comments.
================
Comment at: tools/libclang/CIndex.cpp:3892-3922
CXEvalResult clang_Cursor_Evaluate(CXCursor C) {
- const Decl *D = getCursorDecl(C);
- if (D) {
- const Expr *expr = nullptr;
- if (auto *Var = dyn_cast<VarDecl>(D)) {
- expr = Var->getInit();
- } else if (auto *Field = dyn_cast<FieldDecl>(D)) {
- expr = Field->getInClassInitializer();
+ if (clang_isDeclaration(C.kind)) {
+ const Decl *D = getCursorDecl(C);
+ if (D) {
+ const Expr *expr = nullptr;
+ if (auto *Var = dyn_cast<VarDecl>(D)) {
+ expr = Var->getInit();
----------------
dexonsmith wrote:
> There's unfortunate nesting here. It would be nice if, either pre-commit or post-commit, you could refactor this to be more straightline using early returns. E.g.:
> ```
> static CXEvalResult evaluateDeclExpr(const Decl *D) {
> if (!D)
> return nullptr;
> // ...
> }
> static CXEvalResult evaluateCompoundStmtExpr(const CompoundStmt *CS) {
> if (!CS)
> return nullptr;
> // ...
> }
> CXEvalResult clang_Cursor_Evaluate(CXCursor C) {
> if (clang_isDeclaration(C.kind)) {
> return evaluateDeclExpr(getCursorDecl(C));
> return evaluateCompoundStmtExpr(
> dyn_cast_or_null<CompoundStmt>(getCursorStmt(C)));
> }
> ```
Thanks, will do post commit.
Repository:
rC Clang
https://reviews.llvm.org/D49051
More information about the cfe-commits
mailing list