|
| 1 | +#include "clangmetatool-testconfig.h" |
| 2 | + |
| 3 | +#include <functional> |
| 4 | +#include <sstream> |
| 5 | +#include <string> |
| 6 | +#include <vector> |
| 7 | +#include <utility> |
| 8 | + |
| 9 | +#include <clang/ASTMatchers/ASTMatchers.h> |
| 10 | +#include <clang/ASTMatchers/ASTMatchFinder.h> |
| 11 | +#include <clang/Frontend/FrontendAction.h> |
| 12 | +#include <clang/Tooling/Core/Replacement.h> |
| 13 | +#include <clang/Tooling/CommonOptionsParser.h> |
| 14 | +#include <clang/Tooling/Tooling.h> |
| 15 | +#include <clang/Tooling/Refactoring.h> |
| 16 | +#include <llvm/Support/CommandLine.h> |
| 17 | +#include <clang/Basic/SourceManager.h> |
| 18 | + |
| 19 | +#include <clangmetatool/match_forwarder.h> |
| 20 | +#include <clangmetatool/meta_tool_factory.h> |
| 21 | +#include <clangmetatool/meta_tool.h> |
| 22 | +#include <clangmetatool/source_util.h> |
| 23 | + |
| 24 | +#include <gtest/gtest.h> |
| 25 | + |
| 26 | +namespace { |
| 27 | + |
| 28 | +using namespace clang::ast_matchers; |
| 29 | + |
| 30 | +class MyTool { |
| 31 | +private: |
| 32 | + clang::CompilerInstance* ci; |
| 33 | + clangmetatool::MatchForwarder mf; |
| 34 | + std::vector<const clang::DeclRefExpr*> data; |
| 35 | + |
| 36 | + void handleDeclRefExpr(const MatchFinder::MatchResult& r) { |
| 37 | + data.push_back(r.Nodes.getNodeAs<clang::DeclRefExpr>("ref")); |
| 38 | + } |
| 39 | + |
| 40 | +public: |
| 41 | + MyTool(clang::CompilerInstance* ci, MatchFinder *f) |
| 42 | + : ci(ci), mf(f) { |
| 43 | + using namespace std::placeholders; |
| 44 | + StatementMatcher matcher = declRefExpr().bind("ref"); |
| 45 | + mf.addMatcher(matcher, std::bind(&MyTool::handleDeclRefExpr, this, _1)); |
| 46 | + } |
| 47 | + |
| 48 | + void postProcessing |
| 49 | + (std::map<std::string, clang::tooling::Replacements> &replacementsMap) { |
| 50 | + EXPECT_EQ(data.size(), 3); |
| 51 | + |
| 52 | + std::vector<std::string> expected = { |
| 53 | + "", |
| 54 | + "VAR", |
| 55 | + "VAR", |
| 56 | + }; |
| 57 | + |
| 58 | + for (int i = 0; i < data.size(); ++i) { |
| 59 | + EXPECT_EQ(clangmetatool::SourceUtil::getMacroNameForStatement( |
| 60 | + *data[i], |
| 61 | + ci->getSourceManager()), |
| 62 | + expected[i]); |
| 63 | + } |
| 64 | + } |
| 65 | +}; |
| 66 | + |
| 67 | +} // namespace anonymous |
| 68 | + |
| 69 | +TEST(propagation_MacroConstantPropagation, basic) { |
| 70 | + llvm::cl::OptionCategory MyToolCategory("my-tool options"); |
| 71 | + int argc = 4; |
| 72 | + const char* argv[] = { |
| 73 | + "foo", |
| 74 | + CMAKE_SOURCE_DIR "/t/data/034-macro-name-for-statement/foo.cpp", |
| 75 | + "--", |
| 76 | + "-xc" |
| 77 | + }; |
| 78 | + clang::tooling::CommonOptionsParser optionsParser |
| 79 | + (argc, argv, MyToolCategory); |
| 80 | + clang::tooling::RefactoringTool tool |
| 81 | + (optionsParser.getCompilations(), optionsParser.getSourcePathList()); |
| 82 | + clangmetatool::MetaToolFactory<clangmetatool::MetaTool<MyTool>> |
| 83 | + raf(tool.getReplacements()); |
| 84 | + int r = tool.runAndSave(&raf); |
| 85 | + ASSERT_EQ(0, r); |
| 86 | +} |
| 87 | + |
| 88 | +// ---------------------------------------------------------------------------- |
| 89 | +// Copyright 2018 Bloomberg Finance L.P. |
| 90 | +// |
| 91 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 92 | +// you may not use this file except in compliance with the License. |
| 93 | +// You may obtain a copy of the License at |
| 94 | +// |
| 95 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 96 | +// |
| 97 | +// Unless required by applicable law or agreed to in writing, software |
| 98 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 99 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 100 | +// See the License for the specific language governing permissions and |
| 101 | +// limitations under the License. |
| 102 | +// ----------------------------- END-OF-FILE ---------------------------------- |
0 commit comments