Skip to content

Commit e3a9d09

Browse files
authored
Merge pull request #53 from dbeer1/macro_name_bug
Fix bug in `SourceUtil::getMacroNameForStatement`
2 parents 04a3c1b + fe8b3a0 commit e3a9d09

File tree

4 files changed

+133
-1
lines changed

4 files changed

+133
-1
lines changed

src/source_util.cpp

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -164,7 +164,9 @@ SourceUtil::getSourceForStatement(const clang::Stmt &statement,
164164

165165
std::string SourceUtil::getMacroNameForStatement(
166166
const clang::Stmt &statement, const clang::SourceManager &sourceManager) {
167-
if (sourceManager.isMacroBodyExpansion(statement.getBeginLoc())) {
167+
clang::SourceLocation loc = statement.getBeginLoc();
168+
169+
if (loc.isMacroID()) {
168170
return clang::Lexer::getImmediateMacroName(
169171
statement.getBeginLoc(), sourceManager, clang::LangOptions())
170172
.str();

t/034-macro-name-for-statement.t.cpp

Lines changed: 102 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,102 @@
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 ----------------------------------

t/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@ foreach(
5050
031-validate-include-graph
5151
032-find-functions
5252
033-detect-partial-macro-expansion
53+
034-macro-name-for-statement
5354
)
5455

5556
add_executable(${TEST}.t ${TEST}.t.cpp)
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
int var;
2+
3+
#define VAR var
4+
5+
#define FUNC(x) (x + 1)
6+
7+
int main(){
8+
var;
9+
VAR;
10+
FUNC(VAR);
11+
}
12+
13+
// ----------------------------------------------------------------------------
14+
// Copyright 2018 Bloomberg Finance L.P.
15+
//
16+
// Licensed under the Apache License, Version 2.0 (the "License");
17+
// you may not use this file except in compliance with the License.
18+
// You may obtain a copy of the License at
19+
//
20+
// http://www.apache.org/licenses/LICENSE-2.0
21+
//
22+
// Unless required by applicable law or agreed to in writing, software
23+
// distributed under the License is distributed on an "AS IS" BASIS,
24+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
25+
// See the License for the specific language governing permissions and
26+
// limitations under the License.
27+
// ----------------------------- END-OF-FILE ----------------------------------

0 commit comments

Comments
 (0)