This source file includes following definitions.
- IsAffected
- HandleTopLevelDecl
- CreateASTConsumer
- ParseArgs
#include <algorithm>
#include <cstdio>
#include <map>
#include <sstream>
#include <string>
#include <vector>
#include "clang/AST/AST.h"
#include "clang/AST/ASTConsumer.h"
#include "clang/AST/CharUnits.h"
#include "clang/Basic/SourceManager.h"
#include "clang/Basic/TargetInfo.h"
#include "clang/Frontend/CompilerInstance.h"
#include "clang/Frontend/FrontendPluginRegistry.h"
namespace {
typedef std::vector<std::string> StringVec;
class FindAffectedInterfacesConsumer : public clang::ASTConsumer {
public:
explicit FindAffectedInterfacesConsumer(const StringVec& changed_classes) {
StringVec::const_iterator iter(changed_classes.begin());
StringVec::const_iterator the_end(changed_classes.end());
for (; iter != the_end; ++iter) {
class_is_affected_map_[*iter] = true;
}
}
private:
typedef std::map<std::string, bool> StringBoolMap;
StringBoolMap class_is_affected_map_;
bool IsAffected(const clang::Type& type_to_check) {
std::string type_string(
type_to_check.getCanonicalTypeInternal().getAsString());
std::pair<StringBoolMap::iterator, bool> iter_success_pair =
class_is_affected_map_.insert(
StringBoolMap::value_type(type_string, false));
StringBoolMap::iterator iter(iter_success_pair.first);
bool successfully_inserted(iter_success_pair.second);
if (successfully_inserted) {
if (type_to_check.isPointerType()) {
const clang::PointerType* pointer_type =
dyn_cast<clang::PointerType>(&type_to_check);
iter->second = IsAffected(*pointer_type->getPointeeType().getTypePtr());
} else if (type_to_check.isFunctionProtoType()) {
const clang::FunctionProtoType* func_type =
dyn_cast<clang::FunctionProtoType>(&type_to_check);
iter->second = IsAffected(*func_type->getResultType().getTypePtr());
if (!iter->second) {
clang::FunctionProtoType::arg_type_iterator arg_iter =
func_type->arg_type_begin();
clang::FunctionProtoType::arg_type_iterator arg_end =
func_type->arg_type_end();
for (; (arg_iter != arg_end) && (!iter->second); ++arg_iter) {
iter->second = IsAffected(*(arg_iter->getTypePtr()));
}
}
} else if (type_to_check.isRecordType()) {
const clang::RecordType* record =
dyn_cast<clang::RecordType>(&type_to_check);
const clang::RecordDecl* decl = record->getDecl();
clang::RecordDecl::field_iterator field_iter(decl->field_begin());
clang::RecordDecl::field_iterator field_end(decl->field_end());
for (; (field_iter != field_end) && (!iter->second); ++field_iter) {
iter->second = IsAffected(*(field_iter->getType().getTypePtr()));
}
}
}
return iter->second;
}
virtual void HandleTopLevelDecl(clang::DeclGroupRef decl_group) {
clang::DeclGroupRef::iterator iter(decl_group.begin());
clang::DeclGroupRef::iterator the_end(decl_group.end());
for (; iter != the_end; ++iter) {
const clang::Decl *decl = *iter;
if (const clang::TypeDecl* type_decl = dyn_cast<clang::TypeDecl>(decl)) {
std::string name(type_decl->getNameAsString());
if (const clang::TagDecl* tag = dyn_cast<clang::TagDecl>(type_decl)) {
if (tag->isDefinition()) {
clang::Type* type = type_decl->getTypeForDecl();
if (IsAffected(*type)) {
std::printf("%s\n", name.c_str());
}
}
} else if (const clang::TypedefDecl* td =
dyn_cast<clang::TypedefDecl>(type_decl)) {
clang::Type* type = td->getUnderlyingType().getTypePtr();
if (IsAffected(*type)) {
std::printf("%s\n", name.c_str());
}
}
}
}
}
};
class FindAffectedInterfacesAction : public clang::PluginASTAction {
public:
FindAffectedInterfacesAction() {}
private:
StringVec types_;
virtual clang::ASTConsumer *CreateASTConsumer(
clang::CompilerInstance &instance, llvm::StringRef ) {
return new FindAffectedInterfacesConsumer(types_);
}
virtual bool ParseArgs(const clang::CompilerInstance& ,
const std::vector<std::string>& args) {
StringVec::const_iterator iter(args.begin()), end(args.end());
for (; iter != end; ++iter) {
std::stringstream stream(*iter);
std::string type_name;
while (std::getline(stream, type_name, ',')) {
types_.push_back(type_name);
}
}
return true;
}
};
}
static clang::FrontendPluginRegistry::Add<FindAffectedInterfacesAction>
X("FindAffectedInterfaces",
"Find interfaces affected by changes to the passes classes.");