aboutsummaryrefslogtreecommitdiffstats
path: root/src/FixItUtils.h
blob: 29d3f05e6c09d2e1bbb1b5b9cc18f9a6537b6d99 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
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
/*
  This file is part of the clazy static checker.

  Copyright (C) 2015 Klarälvdalens Datakonsult AB, a KDAB Group company, info@kdab.com
  Author: Sérgio Martins <sergio.martins@kdab.com>

  This library is free software; you can redistribute it and/or
  modify it under the terms of the GNU Library General Public
  License as published by the Free Software Foundation; either
  version 2 of the License, or (at your option) any later version.

  This library is distributed in the hope that it will be useful,
  but WITHOUT ANY WARRANTY; without even the implied warranty of
  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  Library General Public License for more details.

  You should have received a copy of the GNU Library General Public License
  along with this library; see the file COPYING.LIB.  If not, write to
  the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
  Boston, MA 02110-1301, USA.
*/

#ifndef CLAZY_FIXIT_UTILS_H
#define CLAZY_FIXIT_UTILS_H
#include "clazy_export.h"

#include <clang/Parse/Parser.h>
#include <clang/Basic/TokenKinds.h>

#include <string>
#include <vector>

namespace clang {
class ASTContext;
class FixItHint;
class SourceManager;
class SourceRange;
class SourceLocation;
class StringLiteral;
class CallExpr;
class CXXMemberCallExpr;
class Stmt;
}

namespace clazy {

/**
 * Replaces whatever is in range, with replacement
 */
CLAZYLIB_EXPORT clang::FixItHint createReplacement(clang::SourceRange range, const std::string &replacement);

/**
 * Inserts insertion at start
 */
CLAZYLIB_EXPORT clang::FixItHint createInsertion(clang::SourceLocation start, const std::string &insertion);

/**
 * Transforms foo into method(foo), by inserting "method(" at the beginning, and ')' at the end
 */
CLAZYLIB_EXPORT void insertParentMethodCall(const std::string &method, clang::SourceRange range, std::vector<clang::FixItHint> &fixits);

/**
 * Transforms foo into method("literal"), by inserting "method(" at the beginning, and ')' at the end
 * Takes into account multi-token literals such as "foo""bar"
 */
CLAZYLIB_EXPORT bool insertParentMethodCallAroundStringLiteral(const clang::ASTContext *context, const std::string &method, clang::StringLiteral *lt, std::vector<clang::FixItHint> &fixits);

/**
 * Returns the range this literal spans. Takes into account multi token literals, such as "foo""bar"
 */
CLAZYLIB_EXPORT clang::SourceRange rangeForLiteral(const clang::ASTContext *context, clang::StringLiteral *);

/**
 * Goes through all children of stmt and finds the biggests source location.
 */
CLAZYLIB_EXPORT clang::SourceLocation biggestSourceLocationInStmt(const clang::SourceManager &sm, clang::Stmt *stmt);

CLAZYLIB_EXPORT clang::SourceLocation locForNextToken(const clang::ASTContext *context, clang::SourceLocation start, clang::tok::TokenKind kind);

/**
 * Returns the end location of the token that starts at start.
 *
 * For example, having this expr:
 * getenv("FOO")
 *
 * ^              // expr->getLocStart()
 *             ^  // expr->getLocEnd()
 *      ^         // clazy::locForEndOfToken(expr->getLocStart())
 */
CLAZYLIB_EXPORT clang::SourceLocation locForEndOfToken(const clang::ASTContext *context, clang::SourceLocation start, int offset = 0);

/**
 * Transforms a call such as: foo("hello").bar() into baz("hello")
 */
CLAZYLIB_EXPORT bool transformTwoCallsIntoOne(const clang::ASTContext *context, clang::CallExpr *foo, clang::CXXMemberCallExpr *bar,
                              const std::string &baz, std::vector<clang::FixItHint> &fixits);


/**
 * Transforms a call such as: foo("hello").bar() into baz()
 * This version basically replaces everything from start to end with baz.
 */
CLAZYLIB_EXPORT bool transformTwoCallsIntoOneV2(const clang::ASTContext *context, clang::CXXMemberCallExpr *bar,
                                const std::string &baz, std::vector<clang::FixItHint> &fixits);

CLAZYLIB_EXPORT clang::FixItHint fixItReplaceWordWithWord(const clang::ASTContext *context, clang::Stmt *begin,
                                          const std::string &replacement, const std::string &replacee);

CLAZYLIB_EXPORT std::vector<clang::FixItHint> fixItRemoveToken(const clang::ASTContext *context,
                                               clang::Stmt *stmt,
                                               bool removeParenthesis);

}

#endif