|
| 1 | +//===---- InlinableText.cpp - Extract inlinable source text -----*- C++ -*-===// |
| 2 | +// |
| 3 | +// This source file is part of the Swift.org open source project |
| 4 | +// |
| 5 | +// Copyright (c) 2014 - 2018 Apple Inc. and the Swift project authors |
| 6 | +// Licensed under Apache License v2.0 with Runtime Library Exception |
| 7 | +// |
| 8 | +// See https://swift.org/LICENSE.txt for license information |
| 9 | +// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors |
| 10 | +// |
| 11 | +//===----------------------------------------------------------------------===// |
| 12 | +#include "InlinableText.h" |
| 13 | +#include "swift/AST/ASTContext.h" |
| 14 | +#include "swift/AST/ASTWalker.h" |
| 15 | +#include "swift/AST/ASTNode.h" |
| 16 | +#include "swift/AST/Decl.h" |
| 17 | +#include "swift/Parse/Lexer.h" |
| 18 | + |
| 19 | +#include "llvm/ADT/SmallVector.h" |
| 20 | +#include "llvm/ADT/SmallString.h" |
| 21 | + |
| 22 | +using namespace swift; |
| 23 | + |
| 24 | +/// Gets the last token that exists inside this IfConfigClause, ignoring |
| 25 | +/// hoisted elements. |
| 26 | +/// |
| 27 | +/// If the clause is the last element, this returns the beginning of the line |
| 28 | +/// before the parent IfConfigDecl's #endif token. Otherwise, it's the beginning |
| 29 | +/// of the line before the next clause's #else or #elseif token. |
| 30 | +static SourceLoc |
| 31 | +getEffectiveEndLoc(SourceManager &sourceMgr, const IfConfigClause *clause, |
| 32 | + const IfConfigDecl *decl) { |
| 33 | + auto clauses = decl->getClauses(); |
| 34 | + if (clause == &clauses.back()) |
| 35 | + return Lexer::getLocForStartOfLine(sourceMgr, decl->getEndLoc()); |
| 36 | + |
| 37 | + assert(clause >= clauses.begin() && clause < clauses.end() && |
| 38 | + "clauses must be contiguous"); |
| 39 | + |
| 40 | + auto *nextClause = clause + 1; |
| 41 | + return Lexer::getLocForStartOfLine(sourceMgr, nextClause->Loc); |
| 42 | +} |
| 43 | + |
| 44 | +namespace { |
| 45 | +/// A walker that searches through #if declarations, finding all text that does |
| 46 | +/// not contribute to the final evaluated AST. |
| 47 | +/// |
| 48 | +/// For example, in the following code: |
| 49 | +/// ``` |
| 50 | +/// #if true |
| 51 | +/// print("true") |
| 52 | +/// #else |
| 53 | +/// print("false") |
| 54 | +/// #endif |
| 55 | +/// ``` |
| 56 | +/// ExtractInactiveRanges will return the ranges (with leading newlines) of: |
| 57 | +/// ``` |
| 58 | +/// #if true |
| 59 | +/// #else |
| 60 | +/// print("false") |
| 61 | +/// #endif |
| 62 | +/// ``` |
| 63 | +/// Leaving behind just 'print("true")'s range. |
| 64 | +struct ExtractInactiveRanges : public ASTWalker { |
| 65 | + SmallVector<CharSourceRange, 4> ranges; |
| 66 | + SourceManager &sourceMgr; |
| 67 | + |
| 68 | + explicit ExtractInactiveRanges(SourceManager &sourceMgr) |
| 69 | + : sourceMgr(sourceMgr) {} |
| 70 | + |
| 71 | + /// Adds the two SourceLocs as a CharSourceRange to the set of ignored |
| 72 | + /// ranges. |
| 73 | + /// \note: This assumes each of these locs is a character location, not a |
| 74 | + /// token location. |
| 75 | + void addRange(SourceLoc start, SourceLoc end) { |
| 76 | + auto charRange = CharSourceRange(sourceMgr, start, end); |
| 77 | + ranges.push_back(charRange); |
| 78 | + } |
| 79 | + |
| 80 | + bool walkToDeclPre(Decl *d) { |
| 81 | + auto icd = dyn_cast<IfConfigDecl>(d); |
| 82 | + if (!icd) return true; |
| 83 | + |
| 84 | + auto start = Lexer::getLocForStartOfLine(sourceMgr, icd->getStartLoc()); |
| 85 | + auto end = Lexer::getLocForEndOfLine(sourceMgr, icd->getEndLoc()); |
| 86 | + |
| 87 | + auto clause = icd->getActiveClause(); |
| 88 | + |
| 89 | + // If there's no active clause, add the entire #if...#endif block. |
| 90 | + if (!clause) { |
| 91 | + addRange(start, end); |
| 92 | + return false; |
| 93 | + } |
| 94 | + |
| 95 | + // Ignore range from beginning of '#if' to the beginning of the elements |
| 96 | + // of this clause. |
| 97 | + addRange(start, Lexer::getLocForEndOfLine(sourceMgr, clause->Loc)); |
| 98 | + |
| 99 | + // Ignore range from effective end of the elements of this clause to the |
| 100 | + // end of the '#endif' |
| 101 | + addRange(getEffectiveEndLoc(sourceMgr, clause, icd), end); |
| 102 | + |
| 103 | + // Walk into direct children of this node that are IfConfigDecls, because |
| 104 | + // the standard walker won't walk into them. |
| 105 | + for (auto &elt : clause->Elements) |
| 106 | + if (elt.isDecl(DeclKind::IfConfig)) |
| 107 | + elt.get<Decl *>()->walk(*this); |
| 108 | + |
| 109 | + return false; |
| 110 | + } |
| 111 | + |
| 112 | + /// Gets the ignored ranges in source order. |
| 113 | + ArrayRef<CharSourceRange> getSortedRanges() { |
| 114 | + std::sort(ranges.begin(), ranges.end(), |
| 115 | + [&](CharSourceRange r1, CharSourceRange r2) { |
| 116 | + assert(!r1.overlaps(r2) && "no overlapping ranges"); |
| 117 | + return sourceMgr.isBeforeInBuffer(r1.getStart(), r2.getStart()); |
| 118 | + }); |
| 119 | + return ranges; |
| 120 | + } |
| 121 | +}; |
| 122 | +} // end anonymous namespace |
| 123 | + |
| 124 | +StringRef swift::extractInlinableText(SourceManager &sourceMgr, ASTNode node, |
| 125 | + SmallVectorImpl<char> &scratch) { |
| 126 | + // Extract inactive ranges from the text of the node. |
| 127 | + ExtractInactiveRanges extractor(sourceMgr); |
| 128 | + node.walk(extractor); |
| 129 | + |
| 130 | + // If there were no inactive ranges, then there were no #if configs. |
| 131 | + // Return an unowned buffer directly into the source file. |
| 132 | + if (extractor.ranges.empty()) { |
| 133 | + auto range = |
| 134 | + Lexer::getCharSourceRangeFromSourceRange( |
| 135 | + sourceMgr, node.getSourceRange()); |
| 136 | + return sourceMgr.extractText(range); |
| 137 | + } |
| 138 | + |
| 139 | + // Begin piecing together active code ranges. |
| 140 | + |
| 141 | + // Get the full start and end of the provided node, as character locations. |
| 142 | + SourceLoc start = node.getStartLoc(); |
| 143 | + SourceLoc end = Lexer::getLocForEndOfToken(sourceMgr, node.getEndLoc()); |
| 144 | + for (auto &range : extractor.getSortedRanges()) { |
| 145 | + // Add the text from the current 'start' to this ignored range's start. |
| 146 | + auto charRange = CharSourceRange(sourceMgr, start, range.getStart()); |
| 147 | + auto chunk = sourceMgr.extractText(charRange); |
| 148 | + scratch.append(chunk.begin(), chunk.end()); |
| 149 | + |
| 150 | + // Set 'start' to the end of this range, effectively skipping it. |
| 151 | + start = range.getEnd(); |
| 152 | + } |
| 153 | + |
| 154 | + // If there's leftover unignored text, add it. |
| 155 | + if (start != end) { |
| 156 | + auto range = CharSourceRange(sourceMgr, start, end); |
| 157 | + auto chunk = sourceMgr.extractText(range); |
| 158 | + scratch.append(chunk.begin(), chunk.end()); |
| 159 | + } |
| 160 | + return { scratch.data(), scratch.size() }; |
| 161 | +} |
0 commit comments