Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions clang/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -879,6 +879,8 @@ if (LLVM_ADD_NATIVE_VISUALIZERS_TO_SOLUTION)
endif()
add_subdirectory(utils/hmaptool)



if(CLANG_BUILT_STANDALONE)
llvm_distribution_add_targets()
process_llvm_pass_plugins()
Expand All @@ -889,3 +891,5 @@ set(CLANG_INSTALL_LIBDIR_BASENAME "lib${CLANG_LIBDIR_SUFFIX}")
configure_file(
${CLANG_SOURCE_DIR}/include/clang/Config/config.h.cmake
${CLANG_BINARY_DIR}/include/clang/Config/config.h)


2 changes: 2 additions & 0 deletions clang/tools/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -52,3 +52,5 @@ add_llvm_external_project(clang-tools-extra extra)
add_clang_subdirectory(libclang)

add_clang_subdirectory(offload-arch)

add_clang_subdirectory(FunctionScopeIdentifier)
10 changes: 10 additions & 0 deletions clang/tools/FunctionScopeIdentifier/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
add_clang_executable(FunctionScopeIdentifier
FunctionScopeIdentifier.cpp
)

target_link_libraries(FunctionScopeIdentifier
PRIVATE
clangTooling
clangBasic
clangASTMatchers
)
117 changes: 117 additions & 0 deletions clang/tools/FunctionScopeIdentifier/FunctionScopeIdentifier.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,117 @@
#include "clang/Tooling/CommonOptionsParser.h"
#include "clang/Tooling/Tooling.h"
#include "clang/Frontend/FrontendActions.h"
#include "clang/Frontend/CompilerInstance.h"
#include "clang/AST/RecursiveASTVisitor.h"
#include "clang/ASTMatchers/ASTMatchers.h"
#include "clang/ASTMatchers/ASTMatchFinder.h"
#include "llvm/Support/CommandLine.h"
#include <map>
#include <set>
#include <sstream>

using namespace clang;
using namespace clang::tooling;
using namespace llvm;
using namespace clang::ast_matchers;

// Create an option category for the tool
static cl::OptionCategory MyToolCategory("function-scope-identifier options");

// Command-line option for line ranges
static cl::opt<std::string> IdentifyRange(
"identify-scope-range",
cl::desc("Comma-separated list of line ranges (e.g., 5-10,15-20)"),
cl::value_desc("line-ranges"),
cl::Required,
cl::cat(MyToolCategory));

// Parse line range string like "5-10,15-20" into vector of pairs
std::vector<std::pair<unsigned, unsigned>> parseRanges(const std::string &rangeStr) {
std::vector<std::pair<unsigned, unsigned>> ranges;
std::stringstream ss(rangeStr);
std::string part;
while (std::getline(ss, part, ',')) {
auto dash = part.find('-');
if (dash != std::string::npos) {
unsigned start = std::stoi(part.substr(0, dash));
unsigned end = std::stoi(part.substr(dash + 1));
ranges.emplace_back(start, end);
}
}
return ranges;
}

// AST matcher callback
class FunctionVisitor : public MatchFinder::MatchCallback {
SourceManager *SM;
std::vector<std::pair<unsigned, unsigned>> TargetRanges;

public:
FunctionVisitor(SourceManager *SM, std::vector<std::pair<unsigned, unsigned>> Ranges)
: SM(SM), TargetRanges(Ranges) {}

void run(const MatchFinder::MatchResult &Result) override {
const FunctionDecl *FD = Result.Nodes.getNodeAs<FunctionDecl>("func");
if (!FD || !FD->hasBody()) return;

SourceLocation startLoc = FD->getBeginLoc();
SourceLocation endLoc = FD->getEndLoc();

unsigned startLine = SM->getSpellingLineNumber(startLoc);
unsigned endLine = SM->getSpellingLineNumber(endLoc);

for (auto &[rangeStart, rangeEnd] : TargetRanges) {
if (rangeEnd < startLine || rangeStart > endLine) continue;

llvm::outs() << "Range " << rangeStart << "-" << rangeEnd << ":\n";
llvm::outs() << "Function: " << FD->getNameInfo().getName().getAsString() << "\n";
llvm::outs() << "Start Line: " << startLine << "\n";
llvm::outs() << "End Line: " << endLine << "\n\n";
}
}
};

// FrontendAction to wrap matchers
class ScopeFrontendAction : public ASTFrontendAction {
public:
std::vector<std::pair<unsigned, unsigned>> Ranges;

ScopeFrontendAction(std::vector<std::pair<unsigned, unsigned>> R) : Ranges(R) {}

std::unique_ptr<ASTConsumer> CreateASTConsumer(CompilerInstance &CI,
StringRef InFile) override {
auto *Finder = new MatchFinder();
auto *Callback = new FunctionVisitor(&CI.getSourceManager(), Ranges);

Finder->addMatcher(functionDecl(isExpansionInMainFile()).bind("func"), Callback);
return Finder->newASTConsumer();
}
};

// Factory to create ScopeFrontendAction with arguments
class ScopeActionFactory : public FrontendActionFactory {
std::vector<std::pair<unsigned, unsigned>> Ranges;

public:
ScopeActionFactory(std::vector<std::pair<unsigned, unsigned>> R) : Ranges(R) {}

std::unique_ptr<FrontendAction> create() override {
return std::make_unique<ScopeFrontendAction>(Ranges);
}
};

int main(int argc, const char **argv) {
auto ExpectedParser = CommonOptionsParser::create(argc, argv, MyToolCategory);
if (!ExpectedParser) {
llvm::errs() << ExpectedParser.takeError();
return 1;
}

CommonOptionsParser &OptionsParser = ExpectedParser.get();
ClangTool Tool(OptionsParser.getCompilations(), OptionsParser.getSourcePathList());

std::vector<std::pair<unsigned, unsigned>> ranges = parseRanges(IdentifyRange);
return Tool.run(new ScopeActionFactory(ranges));
}

11 changes: 11 additions & 0 deletions test-files/test.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
#include <iostream>

void foo() { std::cout << "foo\n"; }

void bar() { std::cout << "bar\n"; }

int main() {
foo();
bar();
return 0;
}
Loading