forked from alpha0010/ClangLib
-
Notifications
You must be signed in to change notification settings - Fork 4
/
translationunit.h
153 lines (136 loc) · 5.14 KB
/
translationunit.h
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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
#ifndef TRANSLATION_UNIT_H
#define TRANSLATION_UNIT_H
#include <clang-c/Index.h>
#include <clang-c/Documentation.h>
#include "clangpluginapi.h"
#include "tokendatabase.h"
#include <map>
#include <algorithm>
unsigned HashToken(CXCompletionString token, wxString& identifier);
struct ClFunctionScope
{
ClFunctionScope( const wxString& l_functionName, const wxString& l_scopeName, const ClTokenPosition& l_startLocation) :
functionName(l_functionName),
scopeName(l_scopeName),
startLocation(l_startLocation)
{}
wxString functionName;
wxString scopeName;
ClTokenPosition startLocation;
};
typedef std::vector<ClFunctionScope> ClFunctionScopeList;
typedef std::map<ClFileId, ClFunctionScopeList> ClFunctionScopeMap;
class ClTranslationUnit
{
public:
ClTranslationUnit(const ClTranslUnitId id);
ClTranslationUnit(const ClTranslUnitId id, CXIndex clIndex);
// move ctor
#if __cplusplus >= 201103L
ClTranslationUnit(ClTranslationUnit&& other);
ClTranslationUnit(const ClTranslationUnit& other) = delete;
#else
ClTranslationUnit(const ClTranslationUnit& other);
#endif
~ClTranslationUnit();
/** @brief Swap 2 translation units. Function used mostly to make sure there is only 1 class that manages the Translation Unit resource.
*
* @param first The first ClTranslationUnit
* @param second The second ClTranslationUnit
* @return friend void
*
*/
friend void swap( ClTranslationUnit& first, ClTranslationUnit& second )
{
using std::swap;
assert( first.m_Id == second.m_Id );
swap(first.m_Id, second.m_Id);
swap(first.m_FileId, second.m_FileId);
swap(first.m_Files, second.m_Files);
swap(first.m_ClIndex, second.m_ClIndex);
swap(first.m_ClTranslUnit, second.m_ClTranslUnit);
swap(first.m_LastCC, second.m_LastCC);
swap(first.m_LastPos.line, second.m_LastPos.line);
swap(first.m_LastPos.column, second.m_LastPos.column);
swap(first.m_LastParsed, second.m_LastParsed);
swap(first.m_FunctionScopes, second.m_FunctionScopes);
}
bool UsesClangIndex( const CXIndex& idx )
{
return idx == m_ClIndex;
}
bool Contains(ClFileId fId)
{
return std::binary_search(m_Files.begin(), m_Files.end(), fId);
}
int GetFileId() const
{
return m_FileId;
}
bool IsEmpty() const
{
return m_Files.empty();
}
bool IsValid() const
{
if (IsEmpty())
return false;
if (m_ClTranslUnit==nullptr)
return false;
if (m_Id < 0)
return false;
return true;
}
ClTranslUnitId GetId() const
{
return m_Id;
}
wxDateTime GetLastParsed() const
{
return m_LastParsed;
}
// note that complete_line and complete_column are 1 index, not 0 index!
CXCodeCompleteResults* CodeCompleteAt( const wxString& complete_filename, const ClTokenPosition& location,
struct CXUnsavedFile* unsaved_files,
unsigned num_unsaved_files );
const CXCompletionResult* GetCCResult(unsigned index);
CXCursor GetTokenAt(const wxString& filename, const ClTokenPosition& location);
void Parse( const wxString& filename, ClFileId FileId, const std::vector<const char*>& args,
const std::map<wxString, wxString>& unsavedFiles );
void Reparse(const std::map<wxString, wxString>& unsavedFiles);
void ProcessAllTokens(ClTokenDatabase& database, std::vector<ClFileId>& out_includeFileList, ClFunctionScopeMap& out_functionScopes) const;
void GetDiagnostics(const wxString& filename, std::vector<ClDiagnostic>& diagnostics);
CXFile GetFileHandle(const wxString& filename) const;
void ExpandDiagnosticSet(CXDiagnosticSet diagSet, const wxString& filename, std::vector<ClDiagnostic>& diagnostics);
void ExpandDiagnostic(CXDiagnostic diag, const wxString& filename, std::vector<ClDiagnostic>& diagnostics);
void SetFiles( const std::vector<ClFileId>& files ){ m_Files = files; }
void UpdateFunctionScopes( const ClFileId fileId, const ClFunctionScopeList& functionScopes );
void GetFunctionScopes( const ClFileId fileId, ClFunctionScopeList& out_functionScopes ){ out_functionScopes = m_FunctionScopes[fileId]; }
private:
ClTranslUnitId m_Id;
ClFileId m_FileId; ///< The file that triggered the creation of this TU
std::vector<ClFileId> m_Files; ///< All files linked to this TU
CXIndex m_ClIndex;
CXTranslationUnit m_ClTranslUnit;
CXCodeCompleteResults* m_LastCC;
struct FilePos
{
FilePos(unsigned ln, unsigned col) :
line(ln), column(col) {}
void Set(unsigned ln, unsigned col)
{
line = ln;
column = col;
}
bool Equals(unsigned ln, unsigned col)
{
return (line == ln && column == col);
}
unsigned line;
unsigned column;
} m_LastPos;
bool m_Occupied; // Sentinel flag
wxDateTime m_LastParsed; // Timestamp when the file was last parsed
ClFunctionScopeMap m_FunctionScopes;
};
#endif // TRANSLATION_UNIT_H