Skip to content

Commit

Permalink
Fix bug with double static annotations
Browse files Browse the repository at this point in the history
  • Loading branch information
thetheodor committed Mar 9, 2023
1 parent 878f400 commit 74458f8
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 0 deletions.
6 changes: 6 additions & 0 deletions src/GlobalStaticMaker.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
#include "GlobalStaticMaker.h"

#include <llvm/ADT/DenseSet.h>
#include <clang/ASTMatchers/ASTMatchers.h>
#include <clang/ASTMatchers/ASTMatchers.h>
#include <clang/Basic/Specifiers.h>
#include <clang/Basic/Version.h>
Expand Down Expand Up @@ -83,6 +85,10 @@ void detail::RuleActionCallback::run(
auto SM = Result.SourceManager;
for (const auto &T : *Edits) {
assert(T.Kind == transformer::EditKind::Range);
auto Line =SM->getSpellingLineNumber(T.Range.getBegin());
if (AnnotatedLines.contains(Line))
continue;
AnnotatedLines.insert(Line);
auto R = tooling::Replacement(*SM, T.Range, T.Replacement);
auto &Replacements = FileToReplacements[std::string(R.getFilePath())];
auto Err = Replacements.add(R);
Expand Down
3 changes: 3 additions & 0 deletions src/GlobalStaticMaker.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
#include <clang/ASTMatchers/ASTMatchFinder.h>
#include <clang/Tooling/Transformer/RewriteRule.h>

#include <llvm/ADT/DenseSet.h>

namespace dead {
namespace detail {

Expand All @@ -20,6 +22,7 @@ class RuleActionCallback
private:
clang::transformer::RewriteRule Rule;
std::map<std::string, clang::tooling::Replacements> &FileToReplacements;
llvm::DenseSet<unsigned> AnnotatedLines;
};
} // namespace detail

Expand Down
26 changes: 26 additions & 0 deletions test/static_global_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -144,3 +144,29 @@ TEST_CASE("MakeGlobalsStatic extern function") {
CAPTURE(Code);
REQUIRE(formatCode(ExpectedCode) == runMakeGlobalsStaticOnCode(Code));
}

TEST_CASE("MakeGlobalsStatic two variables in one line") {
auto Code = R"code(
int a, b;
)code";

auto ExpectedCode = R"code(
static int a, b;
)code";

CAPTURE(Code);
REQUIRE(formatCode(ExpectedCode) == runMakeGlobalsStaticOnCode(Code));
}

TEST_CASE("MakeGlobalsStatic two variables in one line already static") {
auto Code = R"code(
static int a, b;
)code";

auto ExpectedCode = R"code(
static int a, b;
)code";

CAPTURE(Code);
REQUIRE(formatCode(ExpectedCode) == runMakeGlobalsStaticOnCode(Code));
}

0 comments on commit 74458f8

Please sign in to comment.