File indexing completed on 2024-04-28 16:57:50
0001 /* 0002 This file is part of the clazy static checker. 0003 0004 Copyright (C) 2015 Klarälvdalens Datakonsult AB, a KDAB Group company, info@kdab.com 0005 Author: Sérgio Martins <sergio.martins@kdab.com> 0006 0007 This library is free software; you can redistribute it and/or 0008 modify it under the terms of the GNU Library General Public 0009 License as published by the Free Software Foundation; either 0010 version 2 of the License, or (at your option) any later version. 0011 0012 This library is distributed in the hope that it will be useful, 0013 but WITHOUT ANY WARRANTY; without even the implied warranty of 0014 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 0015 Library General Public License for more details. 0016 0017 You should have received a copy of the GNU Library General Public License 0018 along with this library; see the file COPYING.LIB. If not, write to 0019 the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, 0020 Boston, MA 02110-1301, USA. 0021 */ 0022 0023 #ifndef CLAZY_FIXIT_UTILS_H 0024 #define CLAZY_FIXIT_UTILS_H 0025 0026 #include <clang/Parse/Parser.h> 0027 #include <clang/Basic/TokenKinds.h> 0028 0029 #include <string> 0030 #include <vector> 0031 0032 namespace clang { 0033 class ASTContext; 0034 class FixItHint; 0035 class SourceManager; 0036 class SourceRange; 0037 class SourceLocation; 0038 class StringLiteral; 0039 class CallExpr; 0040 class CXXMemberCallExpr; 0041 class Stmt; 0042 } 0043 0044 namespace clazy { 0045 0046 /** 0047 * Replaces whatever is in range, with replacement 0048 */ 0049 clang::FixItHint createReplacement(clang::SourceRange range, const std::string &replacement); 0050 0051 /** 0052 * Inserts insertion at start 0053 */ 0054 clang::FixItHint createInsertion(clang::SourceLocation start, const std::string &insertion); 0055 0056 /** 0057 * Transforms foo into method(foo), by inserting "method(" at the beginning, and ')' at the end 0058 */ 0059 void insertParentMethodCall(const std::string &method, clang::SourceRange range, std::vector<clang::FixItHint> &fixits); 0060 0061 /** 0062 * Transforms foo into method("literal"), by inserting "method(" at the beginning, and ')' at the end 0063 * Takes into account multi-token literals such as "foo""bar" 0064 */ 0065 bool insertParentMethodCallAroundStringLiteral(const clang::ASTContext *context, const std::string &method, clang::StringLiteral *lt, std::vector<clang::FixItHint> &fixits); 0066 0067 /** 0068 * Returns the range this literal spans. Takes into account multi token literals, such as "foo""bar" 0069 */ 0070 clang::SourceRange rangeForLiteral(const clang::ASTContext *context, clang::StringLiteral *); 0071 0072 /** 0073 * Goes through all children of stmt and finds the biggests source location. 0074 */ 0075 clang::SourceLocation biggestSourceLocationInStmt(const clang::SourceManager &sm, clang::Stmt *stmt); 0076 0077 clang::SourceLocation locForNextToken(const clang::ASTContext *context, clang::SourceLocation start, clang::tok::TokenKind kind); 0078 0079 /** 0080 * Returns the end location of the token that starts at start. 0081 * 0082 * For example, having this expr: 0083 * getenv("FOO") 0084 * 0085 * ^ // expr->getLocStart() 0086 * ^ // expr->getLocEnd() 0087 * ^ // clazy::locForEndOfToken(expr->getLocStart()) 0088 */ 0089 clang::SourceLocation locForEndOfToken(const clang::ASTContext *context, clang::SourceLocation start, int offset = 0); 0090 0091 /** 0092 * Transforms a call such as: foo("hello").bar() into baz("hello") 0093 */ 0094 bool transformTwoCallsIntoOne(const clang::ASTContext *context, clang::CallExpr *foo, clang::CXXMemberCallExpr *bar, 0095 const std::string &baz, std::vector<clang::FixItHint> &fixits); 0096 0097 0098 /** 0099 * Transforms a call such as: foo("hello").bar() into baz() 0100 * This version basically replaces everything from start to end with baz. 0101 */ 0102 bool transformTwoCallsIntoOneV2(const clang::ASTContext *context, clang::CXXMemberCallExpr *bar, 0103 const std::string &baz, std::vector<clang::FixItHint> &fixits); 0104 0105 clang::FixItHint fixItReplaceWordWithWord(const clang::ASTContext *context, clang::Stmt *begin, 0106 const std::string &replacement, const std::string &replacee); 0107 0108 std::vector<clang::FixItHint> fixItRemoveToken(const clang::ASTContext *context, 0109 clang::Stmt *stmt, 0110 bool removeParenthesis); 0111 0112 } 0113 0114 #endif 0115