cpppointerdeclarationformatter.cpp 18.2 KB
Newer Older
1 2
/****************************************************************************
**
Eike Ziller's avatar
Eike Ziller committed
3 4
** Copyright (C) 2015 The Qt Company Ltd.
** Contact: http://www.qt.io/licensing
5 6 7 8 9 10 11
**
** This file is part of Qt Creator.
**
** Commercial License Usage
** Licensees holding valid commercial Qt licenses may use this file in
** accordance with the commercial license agreement provided with the
** Software or, alternatively, in accordance with the terms contained in
Eike Ziller's avatar
Eike Ziller committed
12 13
** a written agreement between you and The Qt Company.  For licensing terms and
** conditions see http://www.qt.io/terms-conditions.  For further information
Eike Ziller's avatar
Eike Ziller committed
14
** use the contact form at http://www.qt.io/contact-us.
15 16 17
**
** GNU Lesser General Public License Usage
** Alternatively, this file may be used under the terms of the GNU Lesser
Eike Ziller's avatar
Eike Ziller committed
18 19 20 21 22 23
** General Public License version 2.1 or version 3 as published by the Free
** Software Foundation and appearing in the file LICENSE.LGPLv21 and
** LICENSE.LGPLv3 included in the packaging of this file.  Please review the
** following information to ensure the GNU Lesser General Public License
** requirements will be met: https://www.gnu.org/licenses/lgpl.html and
** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
24
**
Eike Ziller's avatar
Eike Ziller committed
25 26
** In addition, as a special exception, The Qt Company gives you certain additional
** rights.  These rights are described in The Qt Company LGPL Exception
27 28 29 30 31 32 33
** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
**
****************************************************************************/


#include "cpppointerdeclarationformatter.h"

34 35 36
#include <cplusplus/Overview.h>

#include <QDebug>
37 38 39
#include <QTextCursor>

#define DEBUG_OUTPUT 0
40 41 42 43 44 45 46 47 48 49 50 51 52 53

#if DEBUG_OUTPUT
#  include <typeinfo>
#  ifdef __GNUC__
#    include <cxxabi.h>
#  endif
#endif

#define CHECK_RV(cond, err, r) \
    if (!(cond)) { if (DEBUG_OUTPUT) qDebug() << "Discarded:" << (err); return r; }
#define CHECK_R(cond, err) \
    if (!(cond)) { if (DEBUG_OUTPUT) qDebug() << "Discarded:" << (err); return; }
#define CHECK_C(cond, err) \
    if (!(cond)) { if (DEBUG_OUTPUT) qDebug() << "Discarded:" << (err); continue; }
54 55 56 57

using namespace CppTools;

/*!
Leena Miettinen's avatar
Leena Miettinen committed
58
   Skips specifiers that are not type relevant and returns the index of the
59 60 61 62 63 64 65 66
          first specifier token which is not followed by __attribute__
          ((T___ATTRIBUTE__)).

   This is used to get 'correct' start of the activation range in
   simple declarations.

   Consider these cases:

Leena Miettinen's avatar
Leena Miettinen committed
67 68 69 70 71
    \list
        \li \c {static char *s = 0;}
        \li \c {typedef char *s cp;}
        \li \c {__attribute__((visibility("default"))) char *f();}
    \endlist
72

Leena Miettinen's avatar
Leena Miettinen committed
73 74
   For all these cases we want to skip all the specifiers that are not type
   relevant
75 76
   (since these are not part of the type and thus are not rewritten).

Leena Miettinen's avatar
Leena Miettinen committed
77 78 79 80
   \a list is the specifier list to iterate and \a translationUnit is the
   translation unit.
   \a endToken is the last token to check.
   \a found is an output parameter that must not be 0.
81 82 83 84 85
 */
static unsigned firstTypeSpecifierWithoutFollowingAttribute(
    SpecifierListAST *list, TranslationUnit *translationUnit, unsigned endToken, bool *found)
{
    *found = false;
86
    if (!list || !translationUnit || !endToken)
87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123
        return 0;

    for (SpecifierListAST *it = list; it; it = it->next) {
        SpecifierAST *specifier = it->value;
        CHECK_RV(specifier, "No specifier", 0);
        const unsigned index = specifier->firstToken();
        CHECK_RV(index < endToken, "EndToken reached", 0);

        const int tokenKind = translationUnit->tokenKind(index);
        switch (tokenKind) {
        case T_VIRTUAL:
        case T_INLINE:
        case T_FRIEND:
        case T_REGISTER:
        case T_STATIC:
        case T_EXTERN:
        case T_MUTABLE:
        case T_TYPEDEF:
        case T_CONSTEXPR:
        case T___ATTRIBUTE__:
            continue;
        default:
            // Check if attributes follow
            for (unsigned i = index; i <= endToken; ++i) {
                const int tokenKind = translationUnit->tokenKind(i);
                if (tokenKind == T___ATTRIBUTE__)
                    return 0;
            }
            *found = true;
            return index;
        }
    }

    return 0;
}

PointerDeclarationFormatter::PointerDeclarationFormatter(
124
        const CppRefactoringFilePtr &refactoringFile,
125
        Overview &overview,
126 127 128 129 130 131 132 133 134 135 136 137 138 139 140
        CursorHandling cursorHandling)
    : ASTVisitor(refactoringFile->cppDocument()->translationUnit())
    , m_cppRefactoringFile(refactoringFile)
    , m_overview(overview)
    , m_cursorHandling(cursorHandling)
{}

/*!
    Handle
      (1) Simple declarations like in "char *s, *t, *int foo();"
      (2) Return types of function declarations.
 */
bool PointerDeclarationFormatter::visit(SimpleDeclarationAST *ast)
{
    CHECK_RV(ast, "Invalid AST", true);
141 142 143 144 145
    printCandidate(ast);

    const unsigned tokenKind = tokenAt(ast->firstToken()).kind();
    const bool astIsOk = tokenKind != T_CLASS && tokenKind != T_STRUCT && tokenKind != T_ENUM;
    CHECK_RV(astIsOk, "Nothing to do for class/struct/enum", true);
146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165

    DeclaratorListAST *declaratorList = ast->declarator_list;
    CHECK_RV(declaratorList, "No declarator list", true);
    DeclaratorAST *firstDeclarator = declaratorList->value;
    CHECK_RV(firstDeclarator, "No declarator", true);
    CHECK_RV(ast->symbols, "No Symbols", true);
    CHECK_RV(ast->symbols->value, "No Symbol", true);

    List<Symbol *> *sit = ast->symbols;
    DeclaratorListAST *dit = declaratorList;
    for (; sit && dit; sit = sit->next, dit = dit->next) {
        DeclaratorAST *declarator = dit->value;
        Symbol *symbol = sit->value;

        const bool isFirstDeclarator = declarator == firstDeclarator;

        // If were not handling the first declarator, we need to remove
        // characters from the beginning since our rewritten declaration
        // will contain all type specifiers.
        int charactersToRemove = 0;
166
        if (!isFirstDeclarator) {
167 168 169 170 171 172 173 174
            const int startAST = m_cppRefactoringFile->startOf(ast);
            const int startFirstDeclarator = m_cppRefactoringFile->startOf(firstDeclarator);
            CHECK_RV(startAST < startFirstDeclarator, "No specifier", true);
            charactersToRemove = startFirstDeclarator - startAST;
        }

        // Specify activation range
        int lastActivationToken = 0;
175
        TokenRange range;
176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197
        // (2) Handle function declaration's return type
        if (symbol->type()->asFunctionType()) {
            PostfixDeclaratorListAST *pfDeclaratorList = declarator->postfix_declarator_list;
            CHECK_RV(pfDeclaratorList, "No postfix declarator list", true);
            PostfixDeclaratorAST *pfDeclarator = pfDeclaratorList->value;
            CHECK_RV(pfDeclarator, "No postfix declarator", true);
            FunctionDeclaratorAST *functionDeclarator = pfDeclarator->asFunctionDeclarator();
            CHECK_RV(functionDeclarator, "No function declarator", true);
            // End the activation range before the '(' token.
            lastActivationToken = functionDeclarator->lparen_token - 1;

            SpecifierListAST *specifierList = isFirstDeclarator
                ? ast->decl_specifier_list
                : declarator->attribute_list;

            unsigned firstActivationToken = 0;
            bool foundBegin = false;
            firstActivationToken = firstTypeSpecifierWithoutFollowingAttribute(
                        specifierList,
                        m_cppRefactoringFile->cppDocument()->translationUnit(),
                        lastActivationToken,
                        &foundBegin);
198 199
            if (!foundBegin) {
                CHECK_RV(!isFirstDeclarator, "Declaration without attributes not supported", true);
200 201 202
                firstActivationToken = declarator->firstToken();
            }

203
            range.start = firstActivationToken;
204 205 206 207 208 209 210 211 212 213 214

        // (1) Handle 'normal' declarations.
        } else {
            if (isFirstDeclarator) {
                bool foundBegin = false;
                unsigned firstActivationToken = firstTypeSpecifierWithoutFollowingAttribute(
                            ast->decl_specifier_list,
                            m_cppRefactoringFile->cppDocument()->translationUnit(),
                            declarator->firstToken(),
                            &foundBegin);
                CHECK_RV(foundBegin, "Declaration without attributes not supported", true);
215
                range.start = firstActivationToken;
216
            } else {
217
                range.start = declarator->firstToken();
218 219 220 221 222
            }
            lastActivationToken = declarator->equal_token
                ? declarator->equal_token - 1
                : declarator->lastToken() - 1;
        }
223 224

        range.end = lastActivationToken;
225

226
        checkAndRewrite(declarator, symbol, range, charactersToRemove);
227 228 229 230 231 232 233
    }
    return true;
}

/*! Handle return types of function definitions */
bool PointerDeclarationFormatter::visit(FunctionDefinitionAST *ast)
{
234 235 236
    CHECK_RV(ast, "Invalid AST", true);
    printCandidate(ast);

237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257
    DeclaratorAST *declarator = ast->declarator;
    CHECK_RV(declarator, "No declarator", true);
    CHECK_RV(declarator->ptr_operator_list, "No Pointer or references", true);
    Symbol *symbol = ast->symbol;

    PostfixDeclaratorListAST *pfDeclaratorList = declarator->postfix_declarator_list;
    CHECK_RV(pfDeclaratorList, "No postfix declarator list", true);
    PostfixDeclaratorAST *pfDeclarator = pfDeclaratorList->value;
    CHECK_RV(pfDeclarator, "No postfix declarator", true);
    FunctionDeclaratorAST *functionDeclarator = pfDeclarator->asFunctionDeclarator();
    CHECK_RV(functionDeclarator, "No function declarator", true);

    // Specify activation range
    bool foundBegin = false;
    const unsigned lastActivationToken = functionDeclarator->lparen_token - 1;
    const unsigned firstActivationToken = firstTypeSpecifierWithoutFollowingAttribute(
        ast->decl_specifier_list,
        m_cppRefactoringFile->cppDocument()->translationUnit(),
        lastActivationToken,
        &foundBegin);
    CHECK_RV(foundBegin, "Declaration without attributes not supported", true);
258
    TokenRange range(firstActivationToken, lastActivationToken);
259

260
    checkAndRewrite(declarator, symbol, range);
261 262 263 264 265 266
    return true;
}

/*! Handle parameters in function declarations and definitions */
bool PointerDeclarationFormatter::visit(ParameterDeclarationAST *ast)
{
267 268 269
    CHECK_RV(ast, "Invalid AST", true);
    printCandidate(ast);

270 271 272 273 274 275 276 277 278
    DeclaratorAST *declarator = ast->declarator;
    CHECK_RV(declarator, "No declarator", true);
    CHECK_RV(declarator->ptr_operator_list, "No Pointer or references", true);
    Symbol *symbol = ast->symbol;

    // Specify activation range
    const int lastActivationToken = ast->equal_token
        ? ast->equal_token - 1
        : ast->lastToken() - 1;
279
    TokenRange range(ast->firstToken(), lastActivationToken);
280

281
    checkAndRewrite(declarator, symbol, range);
282 283 284 285 286 287
    return true;
}

/*! Handle declaration in foreach statement */
bool PointerDeclarationFormatter::visit(ForeachStatementAST *ast)
{
288 289 290
    CHECK_RV(ast, "Invalid AST", true);
    printCandidate(ast);

291 292 293 294 295 296 297 298 299 300 301 302 303
    DeclaratorAST *declarator = ast->declarator;
    CHECK_RV(declarator, "No declarator", true);
    CHECK_RV(declarator->ptr_operator_list, "No Pointer or references", true);
    CHECK_RV(ast->type_specifier_list, "No type specifier", true);
    SpecifierAST *firstSpecifier = ast->type_specifier_list->value;
    CHECK_RV(firstSpecifier, "No first type specifier", true);
    CHECK_RV(ast->symbol, "No symbol", true);
    Symbol *symbol = ast->symbol->memberAt(0);

    // Specify activation range
    const int lastActivationToken = declarator->equal_token
        ? declarator->equal_token - 1
        : declarator->lastToken() - 1;
304
    TokenRange range(firstSpecifier->firstToken(), lastActivationToken);
305

306
    checkAndRewrite(declarator, symbol, range);
307 308 309 310 311
    return true;
}

bool PointerDeclarationFormatter::visit(IfStatementAST *ast)
{
312 313
    CHECK_RV(ast, "Invalid AST", true);
    printCandidate(ast);
314 315 316 317 318 319
    processIfWhileForStatement(ast->condition, ast->symbol);
    return true;
}

bool PointerDeclarationFormatter::visit(WhileStatementAST *ast)
{
320 321
    CHECK_RV(ast, "Invalid AST", true);
    printCandidate(ast);
322 323 324 325 326 327
    processIfWhileForStatement(ast->condition, ast->symbol);
    return true;
}

bool PointerDeclarationFormatter::visit(ForStatementAST *ast)
{
328 329
    CHECK_RV(ast, "Invalid AST", true);
    printCandidate(ast);
330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358
    processIfWhileForStatement(ast->condition, ast->symbol);
    return true;
}

/*! Handle declaration in if, while and for statements */
void PointerDeclarationFormatter::processIfWhileForStatement(ExpressionAST *expression,
                                                             Symbol *statementSymbol)
{
    CHECK_R(expression, "No expression");
    CHECK_R(statementSymbol, "No symbol");

    ConditionAST *condition = expression->asCondition();
    CHECK_R(condition, "No condition");
    DeclaratorAST *declarator = condition->declarator;
    CHECK_R(declarator, "No declarator");
    CHECK_R(declarator->ptr_operator_list, "No Pointer or references");
    CHECK_R(declarator->equal_token, "No equal token");
    Block *block = statementSymbol->asBlock();
    CHECK_R(block, "No block");
    CHECK_R(block->memberCount() > 0, "No block members");

    // Get the right symbol
    //
    // This is especially important for e.g.
    //
    //    for (char *s = 0; char *t = 0;) {}
    //
    // The declaration for 's' will be handled in visit(SimpleDeclarationAST *ast),
    // so handle declaration for 't' here.
359
    Scope::iterator it = block->memberEnd() - 1;
360 361 362 363 364 365 366
    Symbol *symbol = *it;
    if (symbol && symbol->asScope()) { // True if there is a  "{ ... }" following.
        --it;
        symbol = *it;
    }

    // Specify activation range
367
    TokenRange range(condition->firstToken(), declarator->equal_token - 1);
368

369
    checkAndRewrite(declarator, symbol, range);
370 371 372
}

/*!
Leena Miettinen's avatar
Leena Miettinen committed
373 374
    Performs some further checks and rewrites the type and name of \a symbol
    into the substitution range in the file specified by \a tokenRange.
375
 */
376 377 378
void PointerDeclarationFormatter::checkAndRewrite(DeclaratorAST *declarator,
                                                  Symbol *symbol,
                                                  TokenRange tokenRange,
379 380
                                                  unsigned charactersToRemove)
{
381 382
    CHECK_R(tokenRange.end > 0, "TokenRange invalid1");
    CHECK_R(tokenRange.start < tokenRange.end, "TokenRange invalid2");
383 384
    CHECK_R(symbol, "No symbol");

385 386
    // Check for expanded tokens
    for (unsigned token = tokenRange.start; token <= tokenRange.end; ++token)
387
        CHECK_R(!tokenAt(token).expanded(), "Token is expanded");
388

Orgad Shaneh's avatar
Orgad Shaneh committed
389 390
    Utils::ChangeSet::Range range(m_cppRefactoringFile->startOf(tokenRange.start),
                                  m_cppRefactoringFile->endOf(tokenRange.end));
391 392 393 394

    CHECK_R(range.start >= 0 && range.end > 0, "ChangeRange invalid1");
    CHECK_R(range.start < range.end, "ChangeRange invalid2");

395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416
    // Check range with respect to cursor position / selection
    if (m_cursorHandling == RespectCursor) {
        const QTextCursor cursor = m_cppRefactoringFile->cursor();
        if (cursor.hasSelection()) {
            CHECK_R(cursor.selectionStart() <= range.start, "Change not in selection range");
            CHECK_R(range.end <= cursor.selectionEnd(), "Change not in selection range");
        } else {
            CHECK_R(range.start <= cursor.selectionStart(), "Cursor before activation range");
            CHECK_R(cursor.selectionEnd() <= range.end, "Cursor after activation range");
        }
    }

    FullySpecifiedType type = symbol->type();
    if (Function *function = type->asFunctionType())
        type = function->returnType();

    // Check if pointers or references are involved
    const QString originalDeclaration = m_cppRefactoringFile->textOf(range);
    CHECK_R(originalDeclaration.contains(QLatin1Char('&'))
            || originalDeclaration.contains(QLatin1Char('*')), "No pointer or references");

    // Does the rewritten declaration (part) differs from the original source (part)?
417 418 419
    QString rewrittenDeclaration;
    const Name *name = symbol->name();
    if (name) {
420 421 422 423 424
        if (name->isOperatorNameId()
                || (name->isQualifiedNameId()
                    && name->asQualifiedNameId()->name()->isOperatorNameId())) {
            const QString operatorText = m_cppRefactoringFile->textOf(declarator->core_declarator);
            m_overview.includeWhiteSpaceInOperatorName = operatorText.contains(QLatin1Char(' '));
425 426
        }
    }
427
    rewrittenDeclaration = m_overview.prettyType(type, name);
428
    rewrittenDeclaration.remove(0, charactersToRemove);
429

430
    CHECK_R(originalDeclaration != rewrittenDeclaration, "Rewritten is same as original");
431 432 433
    CHECK_R(rewrittenDeclaration.contains(QLatin1Char('&'))
            || rewrittenDeclaration.contains(QLatin1Char('*')),
            "No pointer or references in rewritten declaration");
434 435

    if (DEBUG_OUTPUT) {
436 437
        qDebug("==> Rewritten: \"%s\" --> \"%s\"", originalDeclaration.toUtf8().constData(),
               rewrittenDeclaration.toUtf8().constData());
438 439 440 441 442 443 444 445 446 447 448 449 450 451 452
    }

    // Creating the replacement in the changeset may fail due to operations
    // in the changeset that overlap with the current range.
    //
    // Consider this case:
    //
    //    void (*foo)(char * s) = 0;
    //
    // First visit(SimpleDeclarationAST *ast) will be called. It creates a
    // replacement that also includes the parameter.
    // Next visit(ParameterDeclarationAST *ast) is called with the
    // original source. It tries to create an replacement operation
    // at this position and fails due to overlapping ranges (the
    // simple declaration range includes parameter declaration range).
Orgad Shaneh's avatar
Orgad Shaneh committed
453
    Utils::ChangeSet change(m_changeSet);
454 455 456 457 458 459
    if (change.replace(range, rewrittenDeclaration))
        m_changeSet = change;
    else if (DEBUG_OUTPUT)
        qDebug() << "Replacement operation failed";
}

460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477
void PointerDeclarationFormatter::printCandidate(AST *ast)
{
#if DEBUG_OUTPUT
    QString tokens;
    for (unsigned token = ast->firstToken(); token < ast->lastToken(); token++)
        tokens += QString::fromLatin1(tokenAt(token).spell()) + QLatin1Char(' ');

#  ifdef __GNUC__
    QByteArray name = abi::__cxa_demangle(typeid(*ast).name(), 0, 0, 0) + 11;
    name.truncate(name.length() - 3);
#  else
    QByteArray name = typeid(*ast).name();
#  endif
    qDebug("--> Candidate: %s: %s", name.constData(), qPrintable(tokens));
#else
    Q_UNUSED(ast)
#endif // DEBUG_OUTPUT
}