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