-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMoveConstantInitToDeclaration.cpp
More file actions
166 lines (151 loc) · 5.87 KB
/
Copy pathMoveConstantInitToDeclaration.cpp
File metadata and controls
166 lines (151 loc) · 5.87 KB
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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
//===--- ReorderCtorInitializer.cpp - clang-tidy --------------------------===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//
#include "MoveConstantInitToDeclaration.h"
#include "clang/AST/ASTContext.h"
#include "clang/AST/Expr.h"
#include "clang/AST/RecursiveASTVisitor.h"
#include "clang/ASTMatchers/ASTMatchFinder.h"
#include "clang/ASTMatchers/ASTMatchers.h"
#include "clang/Tooling/Tooling.h"
#include "clang/Tooling/Transformer/RangeSelector.h"
#include "clang/Tooling/Transformer/RewriteRule.h"
#include "clang/Tooling/Transformer/Transformer.h"
#include <algorithm>
#include <experimental/map>
#include <iostream>
#include <llvm-14/llvm/Support/raw_ostream.h>
#include <sstream>
using namespace clang::ast_matchers;
namespace clang {
namespace tidy {
namespace modernize {
llvm::StringRef getSourceRangeAsString(SourceManager &Sm,
const SourceRange &Range) {
clang::LangOptions Lo;
// get the text from lexer including newlines and other formatting?
auto StartLoc = Sm.getSpellingLoc(Range.getBegin());
auto LastTokenLoc = Sm.getSpellingLoc(Range.getEnd());
auto EndLoc = clang::Lexer::getLocForEndOfToken(LastTokenLoc, 0, Sm, Lo);
auto PrintableRange = clang::SourceRange{StartLoc, EndLoc};
auto Str = clang::Lexer::getSourceText(
clang::CharSourceRange::getCharRange(PrintableRange), Sm,
clang::LangOptions());
return Str;
}
void MoveConstantInitToDeclaration::registerMatchers(MatchFinder *Finder) {
auto Matcher = cxxRecordDecl(isExpansionInMainFile(),
hasDescendant(cxxCtorInitializer()))
.bind("class_with_ctor_init");
Finder->addMatcher(Matcher, this);
}
template <typename T> bool compare_literal(const T &a, const T &b) {
a.getValue().dump();
b.getValue().dump();
return a.getValue() == b.getValue();
}
template <>
bool compare_literal(const clang::StringLiteral &a,
const clang::StringLiteral &b) {
return a.getString() == b.getString();
}
template <typename T>
bool compare(llvm::SmallVector<const CXXCtorInitializer *> Inits) {
auto head = llvm::cast<T>(Inits.front()->getInit());
const bool same_or_none_init_val = [&Inits, &head]() {
if (Inits.front()->getMember()->hasInClassInitializer()) {
auto InitExpr = Inits.front()->getMember()->getInClassInitializer();
// this can be a initializer list, or other kind, we need to match here
if (llvm::isa<InitListExpr>(InitExpr)) {
auto InitList = llvm::cast<InitListExpr>(InitExpr);
if (InitList->getNumInits() == 1) {
auto TheInit = InitList->getInit(0);
if (llvm::isa<T>(TheInit)) {
auto InitValInClass = llvm::cast<T>(TheInit);
return compare_literal(*InitValInClass, *head);
} else {
return false;
}
} else {
return false;
}
} else {
return false;
}
}
return true;
}();
return std::all_of(std::next(Inits.begin()), Inits.end(),
[head](const auto &val) {
return compare_literal(*head,
*llvm::cast<T>(val->getInit()));
}) &&
same_or_none_init_val;
}
void MoveConstantInitToDeclaration::check(
const MatchFinder::MatchResult &Result) {
if (const clang::CXXRecordDecl *FS =
Result.Nodes.getNodeAs<clang::CXXRecordDecl>(
"class_with_ctor_init")) {
clang::SourceManager &Sm = *Result.SourceManager;
std::map<FieldDecl *, llvm::SmallVector<const CXXCtorInitializer *>>
InitWithLiteral;
for (const auto &Ctor : FS->ctors()) {
if (Ctor->getNumCtorInitializers() == 0) {
continue;
}
for (const auto Init : Ctor->inits()) {
if (Init->isBaseInitializer()) {
continue;
}
if (Init->getSourceOrder() < 0) {
continue;
}
if (llvm::isa<clang::IntegerLiteral>(Init->getInit()) ||
llvm::isa<clang::StringLiteral>(Init->getInit()) ||
llvm::isa<clang::FloatingLiteral>(Init->getInit())) {
InitWithLiteral[Init->getMember()].push_back(Init);
}
}
}
// filter out cases where there are multiple inits in different constructors
// of the same field, and they use a different init value.
std::experimental::erase_if(InitWithLiteral, [](const auto &kv) -> bool {
auto &[Field, Inits] = kv;
if (llvm::isa<IntegerLiteral>(Inits.front()->getInit())) {
return !compare<IntegerLiteral>(Inits);
}
if (llvm::isa<clang::StringLiteral>(Inits.front()->getInit())) {
return !compare<clang::StringLiteral>(Inits);
}
if (llvm::isa<clang::FloatingLiteral>(Inits.front()->getInit())) {
return !compare<clang::FloatingLiteral>(Inits);
}
return false;
});
// for the remaining pairs in the map emit fixit.
for (const auto &[Field, Inits] : InitWithLiteral) {
std::stringstream ss;
ss << getSourceRangeAsString(Sm, Field->getSourceRange()).str() << "{"
<< getSourceRangeAsString(Sm,
(Inits.front()->getInit())->getSourceRange())
.str()
<< "}";
auto Diag = diag(Inits.front()->getSourceRange().getBegin(),
"initialise in declaration", DiagnosticIDs::Warning);
for (const auto Init : Inits) {
Diag << FixItHint::CreateRemoval(Init->getSourceRange());
}
if (!Field->hasInClassInitializer()) {
Diag << FixItHint::CreateReplacement(Field->getSourceRange(), ss.str());
}
}
}
}
} // namespace modernize
} // namespace tidy
} // namespace clang