[PATCH] D82954: Fix crash on overloaded postfix unary operators due to invalid sloc

Eduardo Caldas via Phabricator via cfe-commits cfe-commits at lists.llvm.org
Wed Jul 1 06:28:09 PDT 2020


eduucaldas added a reviewer: gribozavr2.
eduucaldas marked an inline comment as done.
eduucaldas added a comment.

Code that reproduces the crash <https://godbolt.org/z/CWVEJ2>
Notice that when using a postfix unary operator ++ one argument is introduced to differ it from its prefix counterpart, but that argument is not used. This phantom argument shows up in the ClangAST in the form of an `IntegerLiteral` with `invalid sloc`. This invalid sloc in a valid AST node was causing the SyntaxTree generation to crash.
We can address this problems at two different levels:

1. At the `TraverseCXXOperatorCallExpr`, by overriding it and replacing the children iteration to not iterate over this phantom argument.
2. At the `WalkUpFromIntegerLiteral`, by skipping the visitation of the phantom node.

We preferred the latter.

1. Cons: We would have to duplicate the implementation of RecursiveASTVisitor in BuildTree, to handle this subtle change in traversal. That would add code complexity.
2. Cons: We are handling a problem of `CXXOperatorCallExpr` in `IntegerLiteral`.

We chose the latter as, anyways, if an AST node has an invalid sloc, it was either a problem with parsing or the node is supposed to be ignored



================
Comment at: clang/include/clang/AST/ExprCXX.h:143-148
+  bool isPostfixUnaryOp() const;
+
+  bool isPrefixUnaryOp() const;
+
+  bool isUnaryOp() const;
+
----------------
Should new additions to CXXOperatorCallExpr go on a different patch?
I'll also add unit tests for those. I'm waiting for review on https://reviews.llvm.org/D82937, to do that.


Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D82954/new/

https://reviews.llvm.org/D82954





More information about the cfe-commits mailing list