ClangHelpers.cpp 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231
  1. // Copyright (C) 2013 Strahinja Val Markovic <val@markovic.io>
  2. //
  3. // This file is part of YouCompleteMe.
  4. //
  5. // YouCompleteMe is free software: you can redistribute it and/or modify
  6. // it under the terms of the GNU General Public License as published by
  7. // the Free Software Foundation, either version 3 of the License, or
  8. // (at your option) any later version.
  9. //
  10. // YouCompleteMe is distributed in the hope that it will be useful,
  11. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. // GNU General Public License for more details.
  14. //
  15. // You should have received a copy of the GNU General Public License
  16. // along with YouCompleteMe. If not, see <http://www.gnu.org/licenses/>.
  17. #include "standard.h"
  18. #include "ClangHelpers.h"
  19. #include "ClangUtils.h"
  20. #include "Utils.h"
  21. #include "UnsavedFile.h"
  22. #include "Location.h"
  23. #include "Range.h"
  24. #include <boost/unordered_map.hpp>
  25. using boost::unordered_map;
  26. namespace YouCompleteMe {
  27. namespace {
  28. // NOTE: The passed in pointer should never be NULL!
  29. std::string FullDiagnosticText( CXDiagnostic cxdiagnostic ) {
  30. std::string full_text = CXStringToString(
  31. clang_formatDiagnostic(
  32. cxdiagnostic,
  33. clang_defaultDiagnosticDisplayOptions() ) );
  34. // Note: clang docs say that a CXDiagnosticSet retrieved with
  35. // clang_getChildDiagnostics do NOT need to be released with
  36. // clang_diposeDiagnosticSet
  37. CXDiagnosticSet diag_set = clang_getChildDiagnostics( cxdiagnostic );
  38. if ( !diag_set )
  39. return full_text;
  40. uint num_child_diagnostics = clang_getNumDiagnosticsInSet( diag_set );
  41. if ( !num_child_diagnostics )
  42. return full_text;
  43. for ( uint i = 0; i < num_child_diagnostics; ++i ) {
  44. CXDiagnostic diagnostic = clang_getDiagnosticInSet( diag_set, i );
  45. if ( !diagnostic )
  46. continue;
  47. full_text.append( FullDiagnosticText( diagnostic ) );
  48. }
  49. return full_text;
  50. }
  51. char DiagnosticSeverityToType( CXDiagnosticSeverity severity ) {
  52. switch ( severity ) {
  53. case CXDiagnostic_Ignored:
  54. case CXDiagnostic_Note:
  55. return 'I';
  56. case CXDiagnostic_Warning:
  57. return 'W';
  58. case CXDiagnostic_Error:
  59. case CXDiagnostic_Fatal:
  60. return 'E';
  61. default:
  62. return 'E';
  63. }
  64. }
  65. // Returns true when the provided completion string is available to the user;
  66. // unavailable completion strings refer to entities that are private/protected,
  67. // deprecated etc.
  68. bool CompletionStringAvailable( CXCompletionString completion_string ) {
  69. if ( !completion_string )
  70. return false;
  71. return clang_getCompletionAvailability( completion_string ) ==
  72. CXAvailability_Available;
  73. }
  74. std::vector< Range > GetRanges( const DiagnosticWrap &diagnostic_wrap ) {
  75. std::vector< Range > ranges;
  76. uint num_ranges = clang_getDiagnosticNumRanges( diagnostic_wrap.get() );
  77. ranges.reserve( num_ranges );
  78. for ( uint i = 0; i < num_ranges; ++i ) {
  79. ranges.push_back(
  80. Range( clang_getDiagnosticRange( diagnostic_wrap.get(), i ) ) );
  81. }
  82. return ranges;
  83. }
  84. Range GetLocationExtent( CXSourceLocation source_location,
  85. CXTranslationUnit translation_unit ) {
  86. // If you think the below code is an idiotic way of getting the source range
  87. // for an identifier at a specific source location, you are not the only one.
  88. // I cannot believe that this is the only way to achieve this with the
  89. // libclang API in a robust way.
  90. // I've tried many simpler ways of doing this and they all fail in various
  91. // situations.
  92. CXSourceRange range = clang_getCursorExtent(
  93. clang_getCursor( translation_unit, source_location ) );
  94. CXToken *tokens;
  95. uint num_tokens;
  96. clang_tokenize( translation_unit, range, &tokens, &num_tokens );
  97. Location location( source_location );
  98. Range final_range;
  99. for ( uint i = 0; i < num_tokens; ++i ) {
  100. Location token_location( clang_getTokenLocation( translation_unit,
  101. tokens[ i ] ) );
  102. if ( token_location == location ) {
  103. std::string name = CXStringToString(
  104. clang_getTokenSpelling( translation_unit, tokens[ i ] ) );
  105. Location end_location = location;
  106. end_location.column_number_ += name.length();
  107. final_range = Range( location, end_location );
  108. break;
  109. }
  110. }
  111. clang_disposeTokens( translation_unit, tokens, num_tokens );
  112. return final_range;
  113. }
  114. } // unnamed namespace
  115. std::vector< CXUnsavedFile > ToCXUnsavedFiles(
  116. const std::vector< UnsavedFile > &unsaved_files ) {
  117. std::vector< CXUnsavedFile > clang_unsaved_files( unsaved_files.size() );
  118. for ( uint i = 0; i < unsaved_files.size(); ++i ) {
  119. clang_unsaved_files[ i ].Filename = unsaved_files[ i ].filename_.c_str();
  120. clang_unsaved_files[ i ].Contents = unsaved_files[ i ].contents_.c_str();
  121. clang_unsaved_files[ i ].Length = unsaved_files[ i ].length_;
  122. }
  123. return clang_unsaved_files;
  124. }
  125. std::vector< CompletionData > ToCompletionDataVector(
  126. CXCodeCompleteResults *results ) {
  127. std::vector< CompletionData > completions;
  128. if ( !results || !results->Results )
  129. return completions;
  130. completions.reserve( results->NumResults );
  131. unordered_map< std::string, uint > seen_data;
  132. for ( uint i = 0; i < results->NumResults; ++i ) {
  133. CXCompletionResult completion_result = results->Results[ i ];
  134. if ( !CompletionStringAvailable( completion_result.CompletionString ) )
  135. continue;
  136. CompletionData data( completion_result );
  137. uint index = GetValueElseInsert( seen_data,
  138. data.original_string_,
  139. completions.size() );
  140. if ( index == completions.size() ) {
  141. completions.push_back( boost::move( data ) );
  142. }
  143. else {
  144. // If we have already seen this completion, then this is an overload of a
  145. // function we have seen. We add the signature of the overload to the
  146. // detailed information.
  147. completions[ index ].detailed_info_
  148. .append( data.return_type_ )
  149. .append( " " )
  150. .append( data.everything_except_return_type_ )
  151. .append( "\n" );
  152. }
  153. }
  154. return completions;
  155. }
  156. Diagnostic BuildDiagnostic( DiagnosticWrap diagnostic_wrap,
  157. CXTranslationUnit translation_unit ) {
  158. Diagnostic diagnostic;
  159. if ( !diagnostic_wrap )
  160. return diagnostic;
  161. diagnostic.kind_ = DiagnosticSeverityToType(
  162. clang_getDiagnosticSeverity( diagnostic_wrap.get() ) );
  163. // If this is an "ignored" diagnostic, there's no point in continuing since we
  164. // won't display those to the user
  165. if ( diagnostic.kind_ == 'I' )
  166. return diagnostic;
  167. CXSourceLocation source_location =
  168. clang_getDiagnosticLocation( diagnostic_wrap.get() );
  169. diagnostic.location_ = Location( source_location );
  170. diagnostic.location_extent_ = GetLocationExtent( source_location,
  171. translation_unit );
  172. diagnostic.ranges_ = GetRanges( diagnostic_wrap );
  173. diagnostic.text_ = CXStringToString(
  174. clang_getDiagnosticSpelling( diagnostic_wrap.get() ) );
  175. diagnostic.long_formatted_text_ = FullDiagnosticText( diagnostic_wrap.get() );
  176. return diagnostic;
  177. }
  178. } // namespace YouCompleteMe