File: ClangHelpers.cpp

package info (click to toggle)
ycmd 0%2B20230103%2Bgitf53e7ac%2Bds-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 5,904 kB
  • sloc: python: 41,740; cpp: 6,055; java: 295; sh: 271; cs: 194; javascript: 150; ansic: 82; makefile: 46; xml: 18; objc: 10
file content (283 lines) | stat: -rw-r--r-- 9,029 bytes parent folder | download | duplicates (4)
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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
// Copyright (C) 2013-2018 ycmd contributors
//
// This file is part of ycmd.
//
// ycmd is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// ycmd is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with ycmd.  If not, see <http://www.gnu.org/licenses/>.

#include "ClangHelpers.h"
#include "ClangUtils.h"
#include "Location.h"
#include "PythonSupport.h"
#include "Range.h"
#include "UnsavedFile.h"
#include "Utils.h"

#include <unordered_map>
#include <utility>

using std::unordered_map;

namespace YouCompleteMe {
namespace {

DiagnosticKind DiagnosticSeverityToType( CXDiagnosticSeverity severity ) {
  switch ( severity ) {
    case CXDiagnostic_Ignored:
    case CXDiagnostic_Note:
      return DiagnosticKind::INFORMATION;

    case CXDiagnostic_Warning:
      return DiagnosticKind::WARNING;

    case CXDiagnostic_Error:
    case CXDiagnostic_Fatal:
    default:
      return DiagnosticKind::ERROR;
  }
}

FixIt BuildDiagnosticFixIt( const std::string& text, CXDiagnostic diagnostic ) {
  FixIt fixit;

  size_t num_chunks = clang_getDiagnosticNumFixIts( diagnostic );
  if ( !num_chunks ) {
    return fixit;
  }

  fixit.chunks.reserve( num_chunks );
  fixit.location = Location( clang_getDiagnosticLocation( diagnostic ) );
  fixit.text = text;

  for ( size_t idx = 0; idx < num_chunks; ++idx ) {
    FixItChunk chunk;
    CXSourceRange range;
    chunk.replacement_text = CXStringToString(
                               clang_getDiagnosticFixIt( diagnostic,
                                                         idx,
                                                         &range ) );

    chunk.range = Range( range );
    fixit.chunks.push_back( chunk );
  }

  return fixit;
}

/// This method generates a FixIt object for the supplied diagnostic, and any
/// child diagnostics (recursively), should a FixIt be available and appends
/// them to fixits.
/// Similarly it populates full_diagnostic_text with a concatenation of the
/// diagnostic text for the supplied diagnostic and each child diagnostic
/// (recursively).
/// Warning: This method is re-entrant (recursive).
void BuildFullDiagnosticDataFromChildren(
  std::string& full_diagnostic_text,
  std::vector< FixIt >& fixits,
  CXDiagnostic diagnostic ) {

  std::string diag_text = CXStringToString( clang_formatDiagnostic(
                              diagnostic,
                              clang_defaultDiagnosticDisplayOptions() ) );

  full_diagnostic_text.append( diag_text );

  // Populate any fixit attached to this diagnostic.
  FixIt fixit = BuildDiagnosticFixIt( diag_text, diagnostic );
  if ( !fixit.chunks.empty() ) {
    fixits.push_back( fixit );
  }

  // Note: clang docs say that a CXDiagnosticSet retrieved with
  // clang_getChildDiagnostics do NOT need to be released with
  // clang_diposeDiagnosticSet
  CXDiagnosticSet diag_set = clang_getChildDiagnostics( diagnostic );

  if ( !diag_set ) {
    return;
  }

  size_t num_child_diagnostics = clang_getNumDiagnosticsInSet( diag_set );

  if ( !num_child_diagnostics ) {
    return;
  }

  for ( size_t i = 0; i < num_child_diagnostics; ++i ) {
    CXDiagnostic child_diag = clang_getDiagnosticInSet( diag_set, i );

    if( !child_diag ) {
      continue;
    }

    full_diagnostic_text.append( "\n" );

    // recurse
    BuildFullDiagnosticDataFromChildren( full_diagnostic_text,
                                         fixits,
                                         child_diag );
  }
}

// Returns true when the provided completion string is available to the user;
// unavailable completion strings refer to entities that are private/protected,
// deprecated etc.
bool CompletionStringAvailable( CXCompletionString completion_string ) {
  return clang_getCompletionAvailability( completion_string ) ==
         CXAvailability_Available;
}


std::vector< Range > GetRanges( const DiagnosticWrap &diagnostic_wrap ) {
  std::vector< Range > ranges;
  size_t num_ranges = clang_getDiagnosticNumRanges( diagnostic_wrap.get() );
  ranges.reserve( num_ranges );

  for ( size_t i = 0; i < num_ranges; ++i ) {
    ranges.emplace_back( clang_getDiagnosticRange( diagnostic_wrap.get(), i ) );
  }

  return ranges;
}


Range GetLocationExtent( CXSourceLocation source_location,
                         CXTranslationUnit translation_unit ) {
  // If you think the below code is an idiotic way of getting the source range
  // for an identifier at a specific source location, you are not the only one.
  // I cannot believe that this is the only way to achieve this with the
  // libclang API in a robust way.
  // I've tried many simpler ways of doing this and they all fail in various
  // situations.

  CXSourceRange range = clang_getRange( source_location, source_location );
  CXToken *tokens;
  unsigned int num_tokens;
  clang_tokenize( translation_unit, range, &tokens, &num_tokens );

  Location location( source_location );
  Range final_range( range );

  for ( size_t i = 0; i < num_tokens; ++i ) {
    CXToken token = tokens[ i ];
    if ( clang_getTokenKind( token ) == CXToken_Comment ) {
      continue;
    }

    Location token_location( clang_getTokenLocation( translation_unit,
                                                     token ) );

    if ( token_location == location ) {
      final_range = Range( clang_getTokenExtent( translation_unit, token ) );
      break;
    }
  }

  clang_disposeTokens( translation_unit, tokens, num_tokens );
  return final_range;
}


} // unnamed namespace

std::vector< CXUnsavedFile > ToCXUnsavedFiles(
  const std::vector< UnsavedFile > &unsaved_files ) {
  std::vector< CXUnsavedFile > clang_unsaved_files( unsaved_files.size() );

  for ( size_t i = 0; i < unsaved_files.size(); ++i ) {
    clang_unsaved_files[ i ].Filename = unsaved_files[ i ].filename_.c_str();
    clang_unsaved_files[ i ].Contents = unsaved_files[ i ].contents_.c_str();
    clang_unsaved_files[ i ].Length   = unsaved_files[ i ].length_;
  }

  return clang_unsaved_files;
}


std::vector< CompletionData > ToCompletionDataVector(
  CXCodeCompleteResults *results ) {
  std::vector< CompletionData > completions;

  if ( !results || !results->Results ) {
    return completions;
  }

  completions.reserve( results->NumResults );
  unordered_map< std::string, size_t > seen_data;

  for ( size_t i = 0; i < results->NumResults; ++i ) {
    CXCompletionResult result = results->Results[ i ];
    CXCompletionString completion_string = result.CompletionString;

    if ( !completion_string ||
         !CompletionStringAvailable( completion_string ) ) {
      continue;
    }

    CompletionData data( completion_string, result.CursorKind, results, i );
    size_t index = GetValueElseInsert( seen_data,
                                       data.original_string_,
                                       completions.size() );

    if ( index == completions.size() ) {
      completions.push_back( std::move( data ) );
    } else {
      // If we have already seen this completion, then this is an overload of a
      // function we have seen. We add the signature of the overload to the
      // detailed information.
      completions[ index ].detailed_info_
      .append( data.return_type_ )
      .append( " " )
      .append( data.everything_except_return_type_ )
      .append( "\n" );
    }
  }

  return completions;
}


Diagnostic BuildDiagnostic( const DiagnosticWrap &diagnostic_wrap,
                            CXTranslationUnit translation_unit ) {
  Diagnostic diagnostic;

  if ( !diagnostic_wrap ) {
    return diagnostic;
  }

  diagnostic.kind_ = DiagnosticSeverityToType(
                       clang_getDiagnosticSeverity( diagnostic_wrap.get() ) );

  // If this is an "ignored" diagnostic, there's no point in continuing since we
  // won't display those to the user
  if ( diagnostic.kind_ == DiagnosticKind::INFORMATION ) {
    return diagnostic;
  }

  CXSourceLocation source_location =
    clang_getDiagnosticLocation( diagnostic_wrap.get() );
  diagnostic.location_ = Location( source_location );
  diagnostic.location_extent_ = GetLocationExtent( source_location,
                                                   translation_unit );
  diagnostic.ranges_ = GetRanges( diagnostic_wrap );
  diagnostic.text_ = CXStringToString(
                       clang_getDiagnosticSpelling( diagnostic_wrap.get() ) );

  BuildFullDiagnosticDataFromChildren( diagnostic.long_formatted_text_,
                                       diagnostic.fixits_,
                                       diagnostic_wrap.get() );

  return diagnostic;
}

} // namespace YouCompleteMe