C++: Do not let ASTPath calculate line/column for generated tokens

ASTPath uses TranslationUnit::getPosition(), which returns reasonable
results for:

    1. non-expanded tokens
    2. expanded but not generated tokens

The expanded *and* generated tokens case is not handled since there is
no reasonable mapping from generated tokens to a continuous line/column
information. Consider:

    #define DECLARE_FOO int foo; // Multiple generated tokens
    DECLARE_FOO // ...can be mapped to this line, but to which columns?

Since the result where not valid for the expanded and generated case,
ASTPath took the wrong branches. Avoid this by skipping generated
tokens.

Change-Id: I33a2e0f62917f87d691b19feaeef67b09ea8d563
Task-number: QTCREATORBUG-13386
Task-number: QTCREATORBUG-13390
Reviewed-by: Joerg Bornemann <joerg.bornemann@theqtcompany.com>
Reviewed-by: Orgad Shaneh <orgads@gmail.com>
This commit is contained in:
Nikolai Kosjar
2015-07-30 18:15:07 +02:00
parent e3fbe7e93a
commit 892cb154b2
4 changed files with 70 additions and 4 deletions

View File

@@ -65,8 +65,8 @@ void ASTPath::dump(const QList<AST *> nodes)
bool ASTPath::preVisit(AST *ast)
{
unsigned firstToken = ast->firstToken();
unsigned lastToken = ast->lastToken();
const unsigned firstToken = firstNonGeneratedToken(ast);
const unsigned lastToken = lastNonGeneratedToken(ast);
if (firstToken > 0) {
if (lastToken <= firstToken)
@@ -89,3 +89,24 @@ bool ASTPath::preVisit(AST *ast)
return false;
}
unsigned ASTPath::firstNonGeneratedToken(AST *ast) const
{
const unsigned lastTokenIndex = ast->lastToken();
unsigned tokenIndex = ast->firstToken();
while (tokenIndex <= lastTokenIndex && tokenAt(tokenIndex).generated())
++tokenIndex;
return tokenIndex;
}
unsigned ASTPath::lastNonGeneratedToken(AST *ast) const
{
const unsigned firstTokenIndex = ast->firstToken();
const unsigned lastTokenIndex = ast->lastToken();
unsigned tokenIndex = lastTokenIndex;
while (firstTokenIndex <= tokenIndex && tokenAt(tokenIndex).generated())
--tokenIndex;
return tokenIndex != lastTokenIndex
? tokenIndex + 1
: tokenIndex;
}

View File

@@ -62,7 +62,11 @@ public:
#endif
protected:
virtual bool preVisit(AST *ast);
bool preVisit(AST *ast) override;
private:
unsigned firstNonGeneratedToken(AST *ast) const;
unsigned lastNonGeneratedToken(AST *ast) const;
private:
Document::Ptr _doc;