clang-tools  7.0.0
SourceCode.h
Go to the documentation of this file.
1 //===--- SourceCode.h - Manipulating source code as strings -----*- C++ -*-===//
2 //
3 // The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 //
10 // Various code that examines C++ source code without using heavy AST machinery
11 // (and often not even the lexer). To be used sparingly!
12 //
13 //===----------------------------------------------------------------------===//
14 #ifndef LLVM_CLANG_TOOLS_EXTRA_CLANGD_SOURCECODE_H
15 #define LLVM_CLANG_TOOLS_EXTRA_CLANGD_SOURCECODE_H
16 #include "Protocol.h"
17 #include "clang/Basic/SourceLocation.h"
18 #include "clang/Tooling/Core/Replacement.h"
19 
20 namespace clang {
21 class SourceManager;
22 
23 namespace clangd {
24 
25 /// Turn a [line, column] pair into an offset in Code.
26 ///
27 /// If P.character exceeds the line length, returns the offset at end-of-line.
28 /// (If !AllowColumnsBeyondLineLength, then returns an error instead).
29 /// If the line number is out of range, returns an error.
30 ///
31 /// The returned value is in the range [0, Code.size()].
32 llvm::Expected<size_t>
33 positionToOffset(llvm::StringRef Code, Position P,
34  bool AllowColumnsBeyondLineLength = true);
35 
36 /// Turn an offset in Code into a [line, column] pair.
37 /// The offset must be in range [0, Code.size()].
38 Position offsetToPosition(llvm::StringRef Code, size_t Offset);
39 
40 /// Turn a SourceLocation into a [line, column] pair.
41 /// FIXME: This should return an error if the location is invalid.
42 Position sourceLocToPosition(const SourceManager &SM, SourceLocation Loc);
43 
44 // Converts a half-open clang source range to an LSP range.
45 // Note that clang also uses closed source ranges, which this can't handle!
46 Range halfOpenToRange(const SourceManager &SM, CharSourceRange R);
47 
48 // Converts an offset to a clang line/column (1-based, columns are bytes).
49 // The offset must be in range [0, Code.size()].
50 // Prefer to use SourceManager if one is available.
51 std::pair<size_t, size_t> offsetToClangLineColumn(llvm::StringRef Code,
52  size_t Offset);
53 
54 /// From "a::b::c", return {"a::b::", "c"}. Scope is empty if there's no
55 /// qualifier.
56 std::pair<llvm::StringRef, llvm::StringRef>
57 splitQualifiedName(llvm::StringRef QName);
58 
59 TextEdit replacementToEdit(StringRef Code, const tooling::Replacement &R);
60 
61 std::vector<TextEdit> replacementsToEdits(StringRef Code,
62  const tooling::Replacements &Repls);
63 
64 /// Get the absolute file path of a given file entry.
65 llvm::Optional<std::string> getAbsoluteFilePath(const FileEntry *F,
66  const SourceManager &SourceMgr);
67 } // namespace clangd
68 } // namespace clang
69 #endif
SourceLocation Loc
&#39;#&#39; location in the include directive
std::pair< size_t, size_t > offsetToClangLineColumn(StringRef Code, size_t Offset)
Definition: SourceCode.cpp:155
llvm::Expected< size_t > positionToOffset(StringRef Code, Position P, bool AllowColumnsBeyondLineLength)
Definition: SourceCode.cpp:83
TextEdit replacementToEdit(StringRef Code, const tooling::Replacement &R)
Definition: SourceCode.cpp:173
std::vector< TextEdit > replacementsToEdits(StringRef Code, const tooling::Replacements &Repls)
Definition: SourceCode.cpp:180
Position sourceLocToPosition(const SourceManager &SM, SourceLocation Loc)
Turn a SourceLocation into a [line, column] pair.
Definition: SourceCode.cpp:130
===– Representation.cpp - ClangDoc Representation --------—*- C++ -*-===//
CharSourceRange Range
SourceRange for the file name.
std::pair< llvm::StringRef, llvm::StringRef > splitQualifiedName(llvm::StringRef QName)
From "a::b::c", return {"a::b::", "c"}.
Definition: SourceCode.cpp:166
llvm::Optional< std::string > getAbsoluteFilePath(const FileEntry *F, const SourceManager &SourceMgr)
Get the absolute file path of a given file entry.
Definition: SourceCode.cpp:189
Range halfOpenToRange(const SourceManager &SM, CharSourceRange R)
Definition: SourceCode.cpp:147
Position offsetToPosition(StringRef Code, size_t Offset)
Definition: SourceCode.cpp:118