1ebbbb812SAlex Lorenz //===--- SourceExtraction.cpp - Clang refactoring library -----------------===//
2ebbbb812SAlex Lorenz //
32946cd70SChandler Carruth // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
42946cd70SChandler Carruth // See https://llvm.org/LICENSE.txt for license information.
52946cd70SChandler Carruth // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6ebbbb812SAlex Lorenz //
7ebbbb812SAlex Lorenz //===----------------------------------------------------------------------===//
8ebbbb812SAlex Lorenz
952d0cfc9SShaurya Gupta #include "clang/Tooling/Refactoring/Extract/SourceExtraction.h"
10ebbbb812SAlex Lorenz #include "clang/AST/Stmt.h"
11ebbbb812SAlex Lorenz #include "clang/AST/StmtCXX.h"
12ebbbb812SAlex Lorenz #include "clang/AST/StmtObjC.h"
13ebbbb812SAlex Lorenz #include "clang/Basic/SourceManager.h"
14ebbbb812SAlex Lorenz #include "clang/Lex/Lexer.h"
15a1580d7bSKazu Hirata #include <optional>
16ebbbb812SAlex Lorenz
17ebbbb812SAlex Lorenz using namespace clang;
18ebbbb812SAlex Lorenz
19ebbbb812SAlex Lorenz namespace {
20ebbbb812SAlex Lorenz
21ebbbb812SAlex Lorenz /// Returns true if the token at the given location is a semicolon.
isSemicolonAtLocation(SourceLocation TokenLoc,const SourceManager & SM,const LangOptions & LangOpts)22ebbbb812SAlex Lorenz bool isSemicolonAtLocation(SourceLocation TokenLoc, const SourceManager &SM,
23ebbbb812SAlex Lorenz const LangOptions &LangOpts) {
24ebbbb812SAlex Lorenz return Lexer::getSourceText(
25ebbbb812SAlex Lorenz CharSourceRange::getTokenRange(TokenLoc, TokenLoc), SM,
26ebbbb812SAlex Lorenz LangOpts) == ";";
27ebbbb812SAlex Lorenz }
28ebbbb812SAlex Lorenz
29ebbbb812SAlex Lorenz /// Returns true if there should be a semicolon after the given statement.
isSemicolonRequiredAfter(const Stmt * S)30ebbbb812SAlex Lorenz bool isSemicolonRequiredAfter(const Stmt *S) {
31ebbbb812SAlex Lorenz if (isa<CompoundStmt>(S))
32ebbbb812SAlex Lorenz return false;
33ebbbb812SAlex Lorenz if (const auto *If = dyn_cast<IfStmt>(S))
34ebbbb812SAlex Lorenz return isSemicolonRequiredAfter(If->getElse() ? If->getElse()
35ebbbb812SAlex Lorenz : If->getThen());
36ebbbb812SAlex Lorenz if (const auto *While = dyn_cast<WhileStmt>(S))
37ebbbb812SAlex Lorenz return isSemicolonRequiredAfter(While->getBody());
38ebbbb812SAlex Lorenz if (const auto *For = dyn_cast<ForStmt>(S))
39ebbbb812SAlex Lorenz return isSemicolonRequiredAfter(For->getBody());
40ebbbb812SAlex Lorenz if (const auto *CXXFor = dyn_cast<CXXForRangeStmt>(S))
41ebbbb812SAlex Lorenz return isSemicolonRequiredAfter(CXXFor->getBody());
42ebbbb812SAlex Lorenz if (const auto *ObjCFor = dyn_cast<ObjCForCollectionStmt>(S))
43ebbbb812SAlex Lorenz return isSemicolonRequiredAfter(ObjCFor->getBody());
447137736eSShaurya Gupta if(const auto *Switch = dyn_cast<SwitchStmt>(S))
457137736eSShaurya Gupta return isSemicolonRequiredAfter(Switch->getBody());
467137736eSShaurya Gupta if(const auto *Case = dyn_cast<SwitchCase>(S))
477137736eSShaurya Gupta return isSemicolonRequiredAfter(Case->getSubStmt());
48ebbbb812SAlex Lorenz switch (S->getStmtClass()) {
4936ca1e63SShaurya Gupta case Stmt::DeclStmtClass:
50ebbbb812SAlex Lorenz case Stmt::CXXTryStmtClass:
51ebbbb812SAlex Lorenz case Stmt::ObjCAtSynchronizedStmtClass:
52ebbbb812SAlex Lorenz case Stmt::ObjCAutoreleasePoolStmtClass:
53ebbbb812SAlex Lorenz case Stmt::ObjCAtTryStmtClass:
54ebbbb812SAlex Lorenz return false;
55ebbbb812SAlex Lorenz default:
56ebbbb812SAlex Lorenz return true;
57ebbbb812SAlex Lorenz }
58ebbbb812SAlex Lorenz }
59ebbbb812SAlex Lorenz
60ebbbb812SAlex Lorenz /// Returns true if the two source locations are on the same line.
areOnSameLine(SourceLocation Loc1,SourceLocation Loc2,const SourceManager & SM)61ebbbb812SAlex Lorenz bool areOnSameLine(SourceLocation Loc1, SourceLocation Loc2,
62ebbbb812SAlex Lorenz const SourceManager &SM) {
63ebbbb812SAlex Lorenz return !Loc1.isMacroID() && !Loc2.isMacroID() &&
64ebbbb812SAlex Lorenz SM.getSpellingLineNumber(Loc1) == SM.getSpellingLineNumber(Loc2);
65ebbbb812SAlex Lorenz }
66ebbbb812SAlex Lorenz
67ebbbb812SAlex Lorenz } // end anonymous namespace
68ebbbb812SAlex Lorenz
69ebbbb812SAlex Lorenz namespace clang {
70ebbbb812SAlex Lorenz namespace tooling {
71ebbbb812SAlex Lorenz
72ebbbb812SAlex Lorenz ExtractionSemicolonPolicy
compute(const Stmt * S,SourceRange & ExtractedRange,const SourceManager & SM,const LangOptions & LangOpts)73ebbbb812SAlex Lorenz ExtractionSemicolonPolicy::compute(const Stmt *S, SourceRange &ExtractedRange,
74ebbbb812SAlex Lorenz const SourceManager &SM,
75ebbbb812SAlex Lorenz const LangOptions &LangOpts) {
76ebbbb812SAlex Lorenz auto neededInExtractedFunction = []() {
77ebbbb812SAlex Lorenz return ExtractionSemicolonPolicy(true, false);
78ebbbb812SAlex Lorenz };
79ebbbb812SAlex Lorenz auto neededInOriginalFunction = []() {
80ebbbb812SAlex Lorenz return ExtractionSemicolonPolicy(false, true);
81ebbbb812SAlex Lorenz };
82ebbbb812SAlex Lorenz
83ebbbb812SAlex Lorenz /// The extracted expression should be terminated with a ';'. The call to
84ebbbb812SAlex Lorenz /// the extracted function will replace this expression, so it won't need
85ebbbb812SAlex Lorenz /// a terminating ';'.
86ebbbb812SAlex Lorenz if (isa<Expr>(S))
87ebbbb812SAlex Lorenz return neededInExtractedFunction();
88ebbbb812SAlex Lorenz
89ebbbb812SAlex Lorenz /// Some statements don't need to be terminated with ';'. The call to the
90ebbbb812SAlex Lorenz /// extracted function will be a standalone statement, so it should be
91ebbbb812SAlex Lorenz /// terminated with a ';'.
92ebbbb812SAlex Lorenz bool NeedsSemi = isSemicolonRequiredAfter(S);
93ebbbb812SAlex Lorenz if (!NeedsSemi)
94ebbbb812SAlex Lorenz return neededInOriginalFunction();
95ebbbb812SAlex Lorenz
96ebbbb812SAlex Lorenz /// Some statements might end at ';'. The extraction will move that ';', so
97ebbbb812SAlex Lorenz /// the call to the extracted function should be terminated with a ';'.
98ebbbb812SAlex Lorenz SourceLocation End = ExtractedRange.getEnd();
99ebbbb812SAlex Lorenz if (isSemicolonAtLocation(End, SM, LangOpts))
100ebbbb812SAlex Lorenz return neededInOriginalFunction();
101ebbbb812SAlex Lorenz
102ebbbb812SAlex Lorenz /// Other statements should generally have a trailing ';'. We can try to find
103ebbbb812SAlex Lorenz /// it and move it together it with the extracted code.
104*6ad0788cSKazu Hirata std::optional<Token> NextToken = Lexer::findNextToken(End, SM, LangOpts);
105ebbbb812SAlex Lorenz if (NextToken && NextToken->is(tok::semi) &&
106ebbbb812SAlex Lorenz areOnSameLine(NextToken->getLocation(), End, SM)) {
107ebbbb812SAlex Lorenz ExtractedRange.setEnd(NextToken->getLocation());
108ebbbb812SAlex Lorenz return neededInOriginalFunction();
109ebbbb812SAlex Lorenz }
110ebbbb812SAlex Lorenz
111ebbbb812SAlex Lorenz /// Otherwise insert semicolons in both places.
112ebbbb812SAlex Lorenz return ExtractionSemicolonPolicy(true, true);
113ebbbb812SAlex Lorenz }
114ebbbb812SAlex Lorenz
115ebbbb812SAlex Lorenz } // end namespace tooling
116ebbbb812SAlex Lorenz } // end namespace clang
117