[PATCH] D49051: [libclang] check that the cursor is declaration before trying to evaluate the cursor like a declaration

Duncan P. N. Exon Smith via Phabricator via cfe-commits cfe-commits at lists.llvm.org
Mon Jul 9 08:25:29 PDT 2018


dexonsmith accepted this revision.
dexonsmith added a comment.
This revision is now accepted and ready to land.

LGTM.



================
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();
----------------
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)));
}
```


Repository:
  rC Clang

https://reviews.llvm.org/D49051





More information about the cfe-commits mailing list