Skip to content

Commit

Permalink
Handle case where AST name has no location
Browse files Browse the repository at this point in the history
In cases where an IASTName has no image location we were getting
NPEs in this code. See javadoc for getImageLocation for cases
when image location can be null. See #251's description for a
full case of when this can happen.

All other calls to IASTName.getImageLocation in CDT were also checked
and this was the only place in the code where the return value
was not checked for null.

Fixes #251
  • Loading branch information
jonahgraham committed Jan 30, 2023
1 parent b7fac2c commit db84fe4
Showing 1 changed file with 9 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -296,6 +296,9 @@ public IStatus runOnAST(ILanguage lang, IASTTranslationUnit ast) throws CoreExce

private void computeMacroArgumentExtent(IASTName name, Position pos) {
IASTImageLocation imageLoc = name.getImageLocation();
if (imageLoc == null) {
return;
}
int startOffset = imageLoc.getNodeOffset();
int endOffset = startOffset + imageLoc.getNodeLength();
// do some black magic to consider field reference expressions
Expand All @@ -314,10 +317,12 @@ private void computeMacroArgumentExtent(IASTName name, Position pos) {
if (ownerExpr instanceof IASTIdExpression) {
IASTName ownerName = ((IASTIdExpression) ownerExpr).getName();
IASTImageLocation ownerImageLoc = ownerName.getImageLocation();
final int nameOffset = ownerImageLoc.getNodeOffset();
// offset should be inside macro expansion
if (nameOffset < startOffset && nameOffset > macroOffset) {
startOffset = nameOffset;
if (ownerImageLoc != null) {
final int nameOffset = ownerImageLoc.getNodeOffset();
// offset should be inside macro expansion
if (nameOffset < startOffset && nameOffset > macroOffset) {
startOffset = nameOffset;
}
}
}
}
Expand Down

0 comments on commit db84fe4

Please sign in to comment.