Files
carbon-lang/migrate_cpp/cpp_refactoring/main.cpp
T
Jon Meow 2344a52e24 Start handling variable declarations (#571)
TODOs in the test for known issues.

I may switch the approach to getting the variable type (on examination, this isn't working quite as well as I'd thought) but for now I think it's okay. I had an earlier approach though that may work better overall -- I'd been thinking this would work better, but as you can see in the null check for type information, I think I missed a key point.

Anyways, what'd really been vexing me was `int i, j` which I think I handle passably well now. There's obviously room for improvement, but given I've been going at this for a couple days now, I thought it best to checkpoint where I was.

This also includes some related framework changes to fix bumps I was running into. Overall the tool should operate a bit more smoothly with these changes. There are still issues with overlapping replacements, but I think it's primarily with range-based for loops which I just need to take some time to fix.
2021-06-23 10:58:32 -07:00

44 lines
1.6 KiB
C++

// Part of the Carbon Language project, under the Apache License v2.0 with LLVM
// Exceptions. See /LICENSE for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
#include "clang/Tooling/CommonOptionsParser.h"
#include "clang/Tooling/Refactoring.h"
#include "migrate_cpp/cpp_refactoring/fn_inserter.h"
#include "migrate_cpp/cpp_refactoring/var_decl.h"
namespace cam = ::clang::ast_matchers;
namespace ct = ::clang::tooling;
// Initialize the files in replacements. Matcher will restrict replacements to
// initialized files.
static void InitReplacements(ct::RefactoringTool* tool) {
auto& files = tool->getFiles();
auto& repl = tool->getReplacements();
for (const auto& path : tool->getSourcePaths()) {
auto file = files.getFile(path);
if (file.getError()) {
llvm::report_fatal_error("Error accessing `" + path +
"`: " + file.getError().message() + "\n");
}
repl.insert({files.getCanonicalName(*file).str(), {}});
}
}
auto main(int argc, const char** argv) -> int {
llvm::cl::OptionCategory category("C++ refactoring options");
auto parser = ct::CommonOptionsParser::create(argc, argv, category);
ct::RefactoringTool tool(parser->getCompilations(),
parser->getSourcePathList());
InitReplacements(&tool);
// Set up AST matcher callbacks.
auto& repl = tool.getReplacements();
cam::MatchFinder finder;
Carbon::FnInserter fn_inserter(repl, &finder);
Carbon::VarDecl var_decl(repl, &finder);
return tool.runAndSave(
clang::tooling::newFrontendActionFactory(&finder).get());
}