Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(13)

Side by Side Diff: tools/clang/rewrite_to_chrome_style/RewriteToChromeStyle.cpp

Issue 1645263002: rewrite_to_chrome_style: tweak macro ignoring logic. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Betterer commetns Created 4 years, 10 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « no previous file | tools/clang/rewrite_to_chrome_style/tests/macros-expected.cc » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright 2015 The Chromium Authors. All rights reserved. 1 // Copyright 2015 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 // 4 //
5 // Changes Blink-style names to Chrome-style names. Currently transforms: 5 // Changes Blink-style names to Chrome-style names. Currently transforms:
6 // fields: 6 // fields:
7 // int m_operationCount => int operation_count_ 7 // int m_operationCount => int operation_count_
8 // variables (including parameters): 8 // variables (including parameters):
9 // int mySuperVariable => int my_super_variable 9 // int mySuperVariable => int my_super_variable
10 // constants: 10 // constants:
(...skipping 189 matching lines...) Expand 10 before | Expand all | Expand 10 after
200 200
201 return true; 201 return true;
202 } 202 }
203 203
204 template <typename Type> 204 template <typename Type>
205 struct TargetNodeTraits; 205 struct TargetNodeTraits;
206 206
207 template <> 207 template <>
208 struct TargetNodeTraits<clang::NamedDecl> { 208 struct TargetNodeTraits<clang::NamedDecl> {
209 static const char kName[]; 209 static const char kName[];
210 static clang::CharSourceRange GetRange(const clang::NamedDecl& decl) { 210 static clang::SourceLocation GetLoc(const clang::NamedDecl& decl) {
211 return clang::CharSourceRange::getTokenRange(decl.getLocation()); 211 return decl.getLocation();
212 } 212 }
213 }; 213 };
214 const char TargetNodeTraits<clang::NamedDecl>::kName[] = "decl"; 214 const char TargetNodeTraits<clang::NamedDecl>::kName[] = "decl";
215 215
216 template <> 216 template <>
217 struct TargetNodeTraits<clang::MemberExpr> { 217 struct TargetNodeTraits<clang::MemberExpr> {
218 static const char kName[]; 218 static const char kName[];
219 static clang::CharSourceRange GetRange(const clang::MemberExpr& expr) { 219 static clang::SourceLocation GetLoc(const clang::MemberExpr& expr) {
220 return clang::CharSourceRange::getTokenRange(expr.getMemberLoc()); 220 return expr.getMemberLoc();
221 } 221 }
222 }; 222 };
223 const char TargetNodeTraits<clang::MemberExpr>::kName[] = "expr"; 223 const char TargetNodeTraits<clang::MemberExpr>::kName[] = "expr";
224 224
225 template <> 225 template <>
226 struct TargetNodeTraits<clang::DeclRefExpr> { 226 struct TargetNodeTraits<clang::DeclRefExpr> {
227 static const char kName[]; 227 static const char kName[];
228 static clang::CharSourceRange GetRange(const clang::DeclRefExpr& expr) { 228 static clang::SourceLocation GetLoc(const clang::DeclRefExpr& expr) {
229 return clang::CharSourceRange::getTokenRange(expr.getLocation()); 229 return expr.getLocation();
230 } 230 }
231 }; 231 };
232 const char TargetNodeTraits<clang::DeclRefExpr>::kName[] = "expr"; 232 const char TargetNodeTraits<clang::DeclRefExpr>::kName[] = "expr";
233 233
234 template <> 234 template <>
235 struct TargetNodeTraits<clang::CXXCtorInitializer> { 235 struct TargetNodeTraits<clang::CXXCtorInitializer> {
236 static const char kName[]; 236 static const char kName[];
237 static clang::CharSourceRange GetRange( 237 static clang::SourceLocation GetLoc(const clang::CXXCtorInitializer& init) {
238 const clang::CXXCtorInitializer& init) {
239 assert(init.isWritten()); 238 assert(init.isWritten());
240 return clang::CharSourceRange::getTokenRange(init.getSourceLocation()); 239 return init.getSourceLocation();
241 } 240 }
242 }; 241 };
243 const char TargetNodeTraits<clang::CXXCtorInitializer>::kName[] = "initializer"; 242 const char TargetNodeTraits<clang::CXXCtorInitializer>::kName[] = "initializer";
244 243
245 template <typename DeclNode, typename TargetNode> 244 template <typename DeclNode, typename TargetNode>
246 class RewriterBase : public MatchFinder::MatchCallback { 245 class RewriterBase : public MatchFinder::MatchCallback {
247 public: 246 public:
248 explicit RewriterBase(Replacements* replacements) 247 explicit RewriterBase(Replacements* replacements)
249 : replacements_(replacements) {} 248 : replacements_(replacements) {}
250 249
251 void run(const MatchFinder::MatchResult& result) override { 250 void run(const MatchFinder::MatchResult& result) override {
252 std::string new_name;
253 const DeclNode* decl = result.Nodes.getNodeAs<DeclNode>("decl"); 251 const DeclNode* decl = result.Nodes.getNodeAs<DeclNode>("decl");
254 clang::ASTContext* context = result.Context; 252 // If the decl originates inside a macro, just skip it completely.
253 clang::SourceLocation decl_loc =
254 TargetNodeTraits<clang::NamedDecl>::GetLoc(*decl);
255 if (decl_loc.isMacroID())
256 return;
255 // If false, there's no name to be renamed. 257 // If false, there's no name to be renamed.
256 if (!decl->getIdentifier()) 258 if (!decl->getIdentifier())
257 return; 259 return;
258 // If false, the name was not suitable for renaming. 260 // If false, the name was not suitable for renaming.
261 clang::ASTContext* context = result.Context;
262 std::string new_name;
259 if (!GetNameForDecl(*decl, *context, new_name)) 263 if (!GetNameForDecl(*decl, *context, new_name))
260 return; 264 return;
261 llvm::StringRef old_name = decl->getName(); 265 llvm::StringRef old_name = decl->getName();
262 if (old_name == new_name) 266 if (old_name == new_name)
263 return; 267 return;
264 clang::CharSourceRange range = TargetNodeTraits<TargetNode>::GetRange( 268 clang::SourceLocation loc = TargetNodeTraits<TargetNode>::GetLoc(
265 *result.Nodes.getNodeAs<TargetNode>( 269 *result.Nodes.getNodeAs<TargetNode>(
266 TargetNodeTraits<TargetNode>::kName)); 270 TargetNodeTraits<TargetNode>::kName));
267 if (range.getBegin().isMacroID() || range.getEnd().isMacroID()) 271 clang::CharSourceRange range = clang::CharSourceRange::getTokenRange(loc);
268 return;
269 replacements_->emplace(*result.SourceManager, range, new_name); 272 replacements_->emplace(*result.SourceManager, range, new_name);
270 replacement_names_.emplace(old_name.str(), std::move(new_name)); 273 replacement_names_.emplace(old_name.str(), std::move(new_name));
271 } 274 }
272 275
273 const std::unordered_map<std::string, std::string>& replacement_names() 276 const std::unordered_map<std::string, std::string>& replacement_names()
274 const { 277 const {
275 return replacement_names_; 278 return replacement_names_;
276 } 279 }
277 280
278 private: 281 private:
(...skipping 318 matching lines...) Expand 10 before | Expand all | Expand 10 after
597 for (const auto& r : replacements) { 600 for (const auto& r : replacements) {
598 std::string replacement_text = r.getReplacementText().str(); 601 std::string replacement_text = r.getReplacementText().str();
599 std::replace(replacement_text.begin(), replacement_text.end(), '\n', '\0'); 602 std::replace(replacement_text.begin(), replacement_text.end(), '\n', '\0');
600 llvm::outs() << "r:::" << r.getFilePath() << ":::" << r.getOffset() 603 llvm::outs() << "r:::" << r.getFilePath() << ":::" << r.getOffset()
601 << ":::" << r.getLength() << ":::" << replacement_text << "\n"; 604 << ":::" << r.getLength() << ":::" << replacement_text << "\n";
602 } 605 }
603 llvm::outs() << "==== END EDITS ====\n"; 606 llvm::outs() << "==== END EDITS ====\n";
604 607
605 return 0; 608 return 0;
606 } 609 }
OLDNEW
« no previous file with comments | « no previous file | tools/clang/rewrite_to_chrome_style/tests/macros-expected.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698