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

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

Issue 1616223002: rewrite_to_chrome_style: Add a lock file when writing replacements. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@rewrite-operators
Patch Set: rewrite-lock: windoze 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 | no next file » | 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:
11 // const int maxThings => const int kMaxThings 11 // const int maxThings => const int kMaxThings
12 // free functions and methods: 12 // free functions and methods:
13 // void doThisThenThat() => void DoThisAndThat() 13 // void doThisThenThat() => void DoThisAndThat()
14 14
15 #include <assert.h> 15 #include <assert.h>
16 #include <algorithm> 16 #include <algorithm>
17 #include <fstream> 17 #include <fstream>
18 #include <memory> 18 #include <memory>
19 #include <string> 19 #include <string>
20 #include <unordered_map> 20 #include <unordered_map>
21 #include <unordered_set> 21 #include <unordered_set>
22 22
23 #if defined(_WIN32)
24 #include <windows.h>
25 #else
26 #include <sys/file.h>
27 #include <unistd.h>
28 #endif
29
23 #include "clang/AST/ASTContext.h" 30 #include "clang/AST/ASTContext.h"
24 #include "clang/ASTMatchers/ASTMatchFinder.h" 31 #include "clang/ASTMatchers/ASTMatchFinder.h"
25 #include "clang/ASTMatchers/ASTMatchers.h" 32 #include "clang/ASTMatchers/ASTMatchers.h"
26 #include "clang/ASTMatchers/ASTMatchersMacros.h" 33 #include "clang/ASTMatchers/ASTMatchersMacros.h"
27 #include "clang/Basic/CharInfo.h" 34 #include "clang/Basic/CharInfo.h"
28 #include "clang/Basic/SourceManager.h" 35 #include "clang/Basic/SourceManager.h"
29 #include "clang/Frontend/FrontendActions.h" 36 #include "clang/Frontend/FrontendActions.h"
30 #include "clang/Lex/Lexer.h" 37 #include "clang/Lex/Lexer.h"
31 #include "clang/Tooling/CommonOptionsParser.h" 38 #include "clang/Tooling/CommonOptionsParser.h"
32 #include "clang/Tooling/Refactoring.h" 39 #include "clang/Tooling/Refactoring.h"
(...skipping 471 matching lines...) Expand 10 before | Expand all | Expand 10 after
504 &replacements); 511 &replacements);
505 match_finder.addMatcher(constructor_initializer_matcher, 512 match_finder.addMatcher(constructor_initializer_matcher,
506 &constructor_initializer_rewriter); 513 &constructor_initializer_rewriter);
507 514
508 std::unique_ptr<clang::tooling::FrontendActionFactory> factory = 515 std::unique_ptr<clang::tooling::FrontendActionFactory> factory =
509 clang::tooling::newFrontendActionFactory(&match_finder); 516 clang::tooling::newFrontendActionFactory(&match_finder);
510 int result = tool.run(factory.get()); 517 int result = tool.run(factory.get());
511 if (result != 0) 518 if (result != 0)
512 return result; 519 return result;
513 520
521 #if defined(_WIN32)
522 HFILE lockfd = CreateFile("rewrite-sym.lock", GENERIC_READ, FILE_SHARE_READ,
523 NULL, OPEN_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);
524 OVERLAPPED lock_out = {};
525 LockFileEx(lockfd, LOCKFILE_EXCLUSIVE_LOCK, 0, 1, 0, &lock_out);
526 #else
527 int lockfd = open("rewrite-sym.lock", O_RDWR | O_CREAT, 0666);
528 while (flock(lockfd, LOCK_EX)) { // :D
529 }
530 #endif
531
514 std::ofstream replacement_db_file("rewrite-sym.txt", 532 std::ofstream replacement_db_file("rewrite-sym.txt",
515 std::ios_base::out | std::ios_base::app); 533 std::ios_base::out | std::ios_base::app);
516 for (const auto& p : field_decl_rewriter.replacement_names()) 534 for (const auto& p : field_decl_rewriter.replacement_names())
517 replacement_db_file << "var:" << p.first << ":" << p.second << "\n"; 535 replacement_db_file << "var:" << p.first << ":" << p.second << "\n";
518 for (const auto& p : var_decl_rewriter.replacement_names()) 536 for (const auto& p : var_decl_rewriter.replacement_names())
519 replacement_db_file << "var:" << p.first << ":" << p.second << "\n"; 537 replacement_db_file << "var:" << p.first << ":" << p.second << "\n";
520 for (const auto& p : function_decl_rewriter.replacement_names()) 538 for (const auto& p : function_decl_rewriter.replacement_names())
521 replacement_db_file << "fun:" << p.first << ":" << p.second << "\n"; 539 replacement_db_file << "fun:" << p.first << ":" << p.second << "\n";
522 for (const auto& p : method_decl_rewriter.replacement_names()) 540 for (const auto& p : method_decl_rewriter.replacement_names())
523 replacement_db_file << "fun:" << p.first << ":" << p.second << "\n"; 541 replacement_db_file << "fun:" << p.first << ":" << p.second << "\n";
524 replacement_db_file.close(); 542 replacement_db_file.close();
525 543
544 #if defined(_WIN32)
545 OVERLAPPED unlock_out = {};
546 UnlockFileEx(lockfd, 0, 1, 0, &unlock_out);
dcheng 2016/01/26 23:41:45 I think (?) you should pass in the OVERLAPPED stru
danakj 2016/01/26 23:43:51 I was worried that it is an inout so I would have
danakj 2016/01/26 23:44:52 Done.
547 CloseHandle(lockfd);
548 #else
549 flock(lockfd, LOCK_UN);
550 close(lockfd);
551 #endif
552
526 // Serialization format is documented in tools/clang/scripts/run_tool.py 553 // Serialization format is documented in tools/clang/scripts/run_tool.py
527 llvm::outs() << "==== BEGIN EDITS ====\n"; 554 llvm::outs() << "==== BEGIN EDITS ====\n";
528 for (const auto& r : replacements) { 555 for (const auto& r : replacements) {
529 std::string replacement_text = r.getReplacementText().str(); 556 std::string replacement_text = r.getReplacementText().str();
530 std::replace(replacement_text.begin(), replacement_text.end(), '\n', '\0'); 557 std::replace(replacement_text.begin(), replacement_text.end(), '\n', '\0');
531 llvm::outs() << "r:::" << r.getFilePath() << ":::" << r.getOffset() 558 llvm::outs() << "r:::" << r.getFilePath() << ":::" << r.getOffset()
532 << ":::" << r.getLength() << ":::" << replacement_text << "\n"; 559 << ":::" << r.getLength() << ":::" << replacement_text << "\n";
533 } 560 }
534 llvm::outs() << "==== END EDITS ====\n"; 561 llvm::outs() << "==== END EDITS ====\n";
535 562
536 return 0; 563 return 0;
537 } 564 }
OLDNEW
« no previous file with comments | « no previous file | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698