#include "clang/Tooling/Refactoring/ASTSelection.h"
#include "clang/AST/LexicallyOrderedRecursiveASTVisitor.h"
#include "clang/Lex/Lexer.h"
#include "llvm/Support/SaveAndRestore.h"
using namespace clang;
using namespace tooling;
namespace {
CharSourceRange getLexicalDeclRange(Decl *D, const SourceManager &SM,
const LangOptions &LangOpts) {
if (!isa<ObjCImplDecl>(D))
return CharSourceRange::getTokenRange(D->getSourceRange());
SourceRange R = D->getSourceRange();
SourceLocation LocAfterEnd = Lexer::findLocationAfterToken(
R.getEnd(), tok::raw_identifier, SM, LangOpts,
false);
return LocAfterEnd.isValid()
? CharSourceRange::getCharRange(R.getBegin(), LocAfterEnd)
: CharSourceRange::getTokenRange(R);
}
class ASTSelectionFinder
: public LexicallyOrderedRecursiveASTVisitor<ASTSelectionFinder> {
public:
ASTSelectionFinder(SourceRange Selection, FileID TargetFile,
const ASTContext &Context)
: LexicallyOrderedRecursiveASTVisitor(Context.getSourceManager()),
SelectionBegin(Selection.getBegin()),
SelectionEnd(Selection.getBegin() == Selection.getEnd()
? SourceLocation()
: Selection.getEnd()),
TargetFile(TargetFile), Context(Context) {
SelectionStack.push_back(
SelectedASTNode(DynTypedNode::create(*Context.getTranslationUnitDecl()),
SourceSelectionKind::None));
}
Optional<SelectedASTNode> getSelectedASTNode() {
assert(SelectionStack.size() == 1 && "stack was not popped");
SelectedASTNode Result = std::move(SelectionStack.back());
SelectionStack.pop_back();
if (Result.Children.empty())
return None;
return std::move(Result);
}
bool TraversePseudoObjectExpr(PseudoObjectExpr *E) {
llvm::SaveAndRestore<bool> LookThrough(LookThroughOpaqueValueExprs, true);
return TraverseStmt(E->getSyntacticForm());
}
bool TraverseOpaqueValueExpr(OpaqueValueExpr *E) {
if (!LookThroughOpaqueValueExprs)
return true;
llvm::SaveAndRestore<bool> LookThrough(LookThroughOpaqueValueExprs, false);
return TraverseStmt(E->getSourceExpr());
}
bool TraverseDecl(Decl *D) {
if (isa<TranslationUnitDecl>(D))
return LexicallyOrderedRecursiveASTVisitor::TraverseDecl(D);
if (D->isImplicit())
return true;
const SourceRange DeclRange = D->getSourceRange();
const SourceManager &SM = Context.getSourceManager();
SourceLocation FileLoc;
if (DeclRange.getBegin().isMacroID() && !DeclRange.getEnd().isMacroID())
FileLoc = DeclRange.getEnd();
else
FileLoc = SM.getSpellingLoc(DeclRange.getBegin());
if (SM.getFileID(FileLoc) != TargetFile)
return true;
SourceSelectionKind SelectionKind =
selectionKindFor(getLexicalDeclRange(D, SM, Context.getLangOpts()));
SelectionStack.push_back(
SelectedASTNode(DynTypedNode::create(*D), SelectionKind));
LexicallyOrderedRecursiveASTVisitor::TraverseDecl(D);
popAndAddToSelectionIfSelected(SelectionKind);
if (DeclRange.getEnd().isValid() &&
SM.isBeforeInTranslationUnit(SelectionEnd.isValid() ? SelectionEnd
: SelectionBegin,
DeclRange.getEnd())) {
return false;
}
return true;
}
bool TraverseStmt(Stmt *S) {
if (!S)
return true;
if (auto *Opaque = dyn_cast<OpaqueValueExpr>(S))
return TraverseOpaqueValueExpr(Opaque);
if (auto *TE = dyn_cast<CXXThisExpr>(S)) {
if (TE->isImplicit())
return true;
}
SourceSelectionKind SelectionKind =
selectionKindFor(CharSourceRange::getTokenRange(S->getSourceRange()));
SelectionStack.push_back(
SelectedASTNode(DynTypedNode::create(*S), SelectionKind));
LexicallyOrderedRecursiveASTVisitor::TraverseStmt(S);
popAndAddToSelectionIfSelected(SelectionKind);
return true;
}
private:
void popAndAddToSelectionIfSelected(SourceSelectionKind SelectionKind) {
SelectedASTNode Node = std::move(SelectionStack.back());
SelectionStack.pop_back();
if (SelectionKind != SourceSelectionKind::None || !Node.Children.empty())
SelectionStack.back().Children.push_back(std::move(Node));
}
SourceSelectionKind selectionKindFor(CharSourceRange Range) {
SourceLocation End = Range.getEnd();
const SourceManager &SM = Context.getSourceManager();
if (Range.isTokenRange())
End = Lexer::getLocForEndOfToken(End, 0, SM, Context.getLangOpts());
if (!SourceLocation::isPairOfFileLocations(Range.getBegin(), End))
return SourceSelectionKind::None;
if (!SelectionEnd.isValid()) {
if (SM.isPointWithin(SelectionBegin, Range.getBegin(), End))
return SourceSelectionKind::ContainsSelection;
return SourceSelectionKind::None;
}
bool HasStart = SM.isPointWithin(SelectionBegin, Range.getBegin(), End);
bool HasEnd = SM.isPointWithin(SelectionEnd, Range.getBegin(), End);
if (HasStart && HasEnd)
return SourceSelectionKind::ContainsSelection;
if (SM.isPointWithin(Range.getBegin(), SelectionBegin, SelectionEnd) &&
SM.isPointWithin(End, SelectionBegin, SelectionEnd))
return SourceSelectionKind::InsideSelection;
if (HasStart && SelectionBegin != End)
return SourceSelectionKind::ContainsSelectionStart;
if (HasEnd && SelectionEnd != Range.getBegin())
return SourceSelectionKind::ContainsSelectionEnd;
return SourceSelectionKind::None;
}
const SourceLocation SelectionBegin, SelectionEnd;
FileID TargetFile;
const ASTContext &Context;
std::vector<SelectedASTNode> SelectionStack;
bool LookThroughOpaqueValueExprs = false;
};
}
Optional<SelectedASTNode>
clang::tooling::findSelectedASTNodes(const ASTContext &Context,
SourceRange SelectionRange) {
assert(SelectionRange.isValid() &&
SourceLocation::isPairOfFileLocations(SelectionRange.getBegin(),
SelectionRange.getEnd()) &&
"Expected a file range");
FileID TargetFile =
Context.getSourceManager().getFileID(SelectionRange.getBegin());
assert(Context.getSourceManager().getFileID(SelectionRange.getEnd()) ==
TargetFile &&
"selection range must span one file");
ASTSelectionFinder Visitor(SelectionRange, TargetFile, Context);
Visitor.TraverseDecl(Context.getTranslationUnitDecl());
return Visitor.getSelectedASTNode();
}
static const char *selectionKindToString(SourceSelectionKind Kind) {
switch (Kind) {
case SourceSelectionKind::None:
return "none";
case SourceSelectionKind::ContainsSelection:
return "contains-selection";
case SourceSelectionKind::ContainsSelectionStart:
return "contains-selection-start";
case SourceSelectionKind::ContainsSelectionEnd:
return "contains-selection-end";
case SourceSelectionKind::InsideSelection:
return "inside";
}
llvm_unreachable("invalid selection kind");
}
static void dump(const SelectedASTNode &Node, llvm::raw_ostream &OS,
unsigned Indent = 0) {
OS.indent(Indent * 2);
if (const Decl *D = Node.Node.get<Decl>()) {
OS << D->getDeclKindName() << "Decl";
if (const auto *ND = dyn_cast<NamedDecl>(D))
OS << " \"" << ND->getDeclName() << '"';
} else if (const Stmt *S = Node.Node.get<Stmt>()) {
OS << S->getStmtClassName();
}
OS << ' ' << selectionKindToString(Node.SelectionKind) << "\n";
for (const auto &Child : Node.Children)
dump(Child, OS, Indent + 1);
}
void SelectedASTNode::dump(llvm::raw_ostream &OS) const { ::dump(*this, OS); }
static bool hasAnyDirectChildrenWithKind(const SelectedASTNode &Node,
SourceSelectionKind Kind) {
assert(Kind != SourceSelectionKind::None && "invalid predicate!");
for (const auto &Child : Node.Children) {
if (Child.SelectionKind == Kind)
return true;
if (Child.SelectionKind == SourceSelectionKind::None)
return hasAnyDirectChildrenWithKind(Child, Kind);
}
return false;
}
namespace {
struct SelectedNodeWithParents {
SelectedASTNode::ReferenceType Node;
llvm::SmallVector<SelectedASTNode::ReferenceType, 8> Parents;
void canonicalize();
};
enum SelectionCanonicalizationAction { KeepSelection, SelectParent };
SelectionCanonicalizationAction
getSelectionCanonizalizationAction(const Stmt *S, const Stmt *Parent) {
if (isa<StringLiteral>(S) && isa<ObjCStringLiteral>(Parent))
return SelectParent;
else if (const auto *CE = dyn_cast<CallExpr>(Parent)) {
if ((isa<MemberExpr>(S) || isa<DeclRefExpr>(S)) &&
CE->getCallee()->IgnoreImpCasts() == S)
return SelectParent;
}
return KeepSelection;
}
}
void SelectedNodeWithParents::canonicalize() {
const Stmt *S = Node.get().Node.get<Stmt>();
assert(S && "non statement selection!");
const Stmt *Parent = Parents[Parents.size() - 1].get().Node.get<Stmt>();
if (!Parent)
return;
unsigned ParentIndex = 1;
for (; (ParentIndex + 1) <= Parents.size() && isa<ImplicitCastExpr>(Parent);
++ParentIndex) {
const Stmt *NewParent =
Parents[Parents.size() - ParentIndex - 1].get().Node.get<Stmt>();
if (!NewParent)
break;
Parent = NewParent;
}
switch (getSelectionCanonizalizationAction(S, Parent)) {
case SelectParent:
Node = Parents[Parents.size() - ParentIndex];
for (; ParentIndex != 0; --ParentIndex)
Parents.pop_back();
break;
case KeepSelection:
break;
}
}
static void findDeepestWithKind(
const SelectedASTNode &ASTSelection,
llvm::SmallVectorImpl<SelectedNodeWithParents> &MatchingNodes,
SourceSelectionKind Kind,
llvm::SmallVectorImpl<SelectedASTNode::ReferenceType> &ParentStack) {
if (ASTSelection.Node.get<DeclStmt>()) {
for (const auto &Child : ASTSelection.Children) {
if (!hasAnyDirectChildrenWithKind(Child, Kind)) {
MatchingNodes.push_back(SelectedNodeWithParents{
std::cref(ASTSelection), {ParentStack.begin(), ParentStack.end()}});
return;
}
}
} else {
if (!hasAnyDirectChildrenWithKind(ASTSelection, Kind)) {
MatchingNodes.push_back(SelectedNodeWithParents{
std::cref(ASTSelection), {ParentStack.begin(), ParentStack.end()}});
return;
}
}
ParentStack.push_back(std::cref(ASTSelection));
for (const auto &Child : ASTSelection.Children)
findDeepestWithKind(Child, MatchingNodes, Kind, ParentStack);
ParentStack.pop_back();
}
static void findDeepestWithKind(
const SelectedASTNode &ASTSelection,
llvm::SmallVectorImpl<SelectedNodeWithParents> &MatchingNodes,
SourceSelectionKind Kind) {
llvm::SmallVector<SelectedASTNode::ReferenceType, 16> ParentStack;
findDeepestWithKind(ASTSelection, MatchingNodes, Kind, ParentStack);
}
Optional<CodeRangeASTSelection>
CodeRangeASTSelection::create(SourceRange SelectionRange,
const SelectedASTNode &ASTSelection) {
if (SelectionRange.getBegin() == SelectionRange.getEnd())
return None;
llvm::SmallVector<SelectedNodeWithParents, 4> ContainSelection;
findDeepestWithKind(ASTSelection, ContainSelection,
SourceSelectionKind::ContainsSelection);
if (ContainSelection.size() != 1)
return None;
SelectedNodeWithParents &Selected = ContainSelection[0];
if (!Selected.Node.get().Node.get<Stmt>())
return None;
const Stmt *CodeRangeStmt = Selected.Node.get().Node.get<Stmt>();
if (!isa<CompoundStmt>(CodeRangeStmt)) {
Selected.canonicalize();
return CodeRangeASTSelection(Selected.Node, Selected.Parents,
false);
}
Selected.Parents.push_back(Selected.Node);
return CodeRangeASTSelection(Selected.Node, Selected.Parents,
true);
}
static bool isFunctionLikeDeclaration(const Decl *D) {
return isa<FunctionDecl>(D) || isa<ObjCMethodDecl>(D);
}
bool CodeRangeASTSelection::isInFunctionLikeBodyOfCode() const {
bool IsPrevCompound = false;
for (const auto &Parent : llvm::reverse(Parents)) {
const DynTypedNode &Node = Parent.get().Node;
if (const auto *D = Node.get<Decl>()) {
if (isFunctionLikeDeclaration(D))
return IsPrevCompound;
if (isa<TypeDecl>(D))
return false;
}
IsPrevCompound = Node.get<CompoundStmt>() != nullptr;
}
return false;
}
const Decl *CodeRangeASTSelection::getFunctionLikeNearestParent() const {
for (const auto &Parent : llvm::reverse(Parents)) {
const DynTypedNode &Node = Parent.get().Node;
if (const auto *D = Node.get<Decl>()) {
if (isFunctionLikeDeclaration(D))
return D;
}
}
return nullptr;
}