-
Notifications
You must be signed in to change notification settings - Fork 10
/
ClangUtils.cpp
89 lines (78 loc) · 3.29 KB
/
ClangUtils.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
//
// Utility functionality for dealing with the clang libraries and
// manipulating clang data structures.
//
// Part of ccons, the interactive console for the C programming language.
//
// Copyright (c) 2009 Alexei Svitkine. This file is distributed under the
// terms of MIT Open Source License. See file LICENSE for details.
//
#include "ClangUtils.h"
#include <clang/AST/AST.h>
#include <clang/Basic/SourceManager.h>
#include <clang/Lex/Lexer.h>
#include <clang/Lex/MacroInfo.h>
#include <clang/Lex/Preprocessor.h>
#include <clang/Sema/SemaDiagnostic.h>
namespace ccons {
// Get the source range of the specified Stmt.
SrcRange getStmtRange(const clang::Stmt *S,
const clang::SourceManager& sm,
const clang::LangOptions& options)
{
clang::SourceLocation SLoc = sm.getExpansionLoc(S->getLocStart());
clang::SourceLocation ELoc = sm.getExpansionLoc(S->getLocEnd());
// This is necessary to get the correct range of function-like macros.
if (SLoc == ELoc && S->getLocEnd().isMacroID())
ELoc = sm.getExpansionRange(S->getLocEnd()).second;
return constructSrcRange(sm, options, SLoc, ELoc);
}
// Get the source range of the specified Stmt, ensuring that a semicolon is
// included, if necessary - since the clang ranges do not guarantee this.
SrcRange getStmtRangeWithSemicolon(const clang::Stmt *S,
const clang::SourceManager& sm,
const clang::LangOptions& options)
{
clang::SourceLocation SLoc = sm.getExpansionLoc(S->getLocStart());
clang::SourceLocation ELoc = sm.getExpansionLoc(S->getLocEnd());
unsigned start = sm.getFileOffset(SLoc);
unsigned end = sm.getFileOffset(ELoc);
// Below code copied from clang::Lexer::MeasureTokenLength():
clang::SourceLocation Loc = sm.getExpansionLoc(ELoc);
std::pair<clang::FileID, unsigned> LocInfo = sm.getDecomposedLoc(Loc);
llvm::StringRef Buffer = sm.getBufferData(LocInfo.first);
const char *StrData = Buffer.data()+LocInfo.second;
clang::Lexer TheLexer(Loc, options, Buffer.begin(), StrData, Buffer.end());
clang::Token TheTok;
TheLexer.LexFromRawLexer(TheTok);
// End copied code.
end += TheTok.getLength();
// Check if we the source range did include the semicolon.
if (TheTok.isNot(clang::tok::semi) && TheTok.isNot(clang::tok::r_brace)) {
TheLexer.LexFromRawLexer(TheTok);
if (TheTok.is(clang::tok::semi)) {
end += TheTok.getLength();
}
}
return SrcRange(start, end);
}
// Get the source range of the macro definition excluding the #define.
SrcRange getMacroRange(const clang::MacroInfo *MI,
const clang::SourceManager& sm,
const clang::LangOptions& options)
{
clang::SourceLocation SLoc = sm.getExpansionLoc(MI->getDefinitionLoc());
clang::SourceLocation ELoc = sm.getExpansionLoc(MI->getDefinitionEndLoc());
return constructSrcRange(sm, options, SLoc, ELoc);
}
SrcRange constructSrcRange(const clang::SourceManager& sm,
const clang::LangOptions& options,
const clang::SourceLocation& SLoc,
const clang::SourceLocation& ELoc)
{
unsigned start = sm.getFileOffset(SLoc);
unsigned end = sm.getFileOffset(ELoc);
end += clang::Lexer::MeasureTokenLength(ELoc, sm, options);
return SrcRange(start, end);
}
} // namespace ccons