From ae57c9c39b5e1d0f5a288f6ec70ca9cf8d1c2d4f Mon Sep 17 00:00:00 2001 From: Strahinja Val Markovic Date: Thu, 6 Sep 2012 12:09:32 -0700 Subject: [PATCH] ClanUtils functions are now all in one place Also, internal ClanUtils functions are now in an unnamed namespace --- cpp/ycm/ClangCompleter/ClangUtils.cpp | 126 ++++++++++++---------- cpp/ycm/ClangCompleter/ClangUtils.h | 2 + cpp/ycm/ClangCompleter/CompletionData.cpp | 13 +-- 3 files changed, 73 insertions(+), 68 deletions(-) diff --git a/cpp/ycm/ClangCompleter/ClangUtils.cpp b/cpp/ycm/ClangCompleter/ClangUtils.cpp index 70f571d9..b13a01b0 100644 --- a/cpp/ycm/ClangCompleter/ClangUtils.cpp +++ b/cpp/ycm/ClangCompleter/ClangUtils.cpp @@ -29,37 +29,51 @@ using boost::unordered_map; namespace YouCompleteMe { -std::vector< CXUnsavedFile > ToCXUnsavedFiles( - const std::vector< UnsavedFile > &unsaved_files ) +std::string CXStringToString( CXString text ) { - std::vector< CXUnsavedFile > clang_unsaved_files( unsaved_files.size() ); - for ( uint i = 0; i < unsaved_files.size(); ++i ) + std::string final_string; + if ( !text.data ) + return final_string; + + final_string = std::string( clang_getCString( text ) ); + clang_disposeString( text ); + return final_string; +} + +namespace +{ + +// NOTE: The passed in pointer should never be NULL! +std::string FullDiagnosticText( CXDiagnostic cxdiagnostic ) +{ + std::string full_text = CXStringToString( clang_formatDiagnostic( + cxdiagnostic, + clang_defaultDiagnosticDisplayOptions() ) ); + + // 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( cxdiagnostic ); + if ( !diag_set ) + return full_text; + + uint num_child_diagnostics = clang_getNumDiagnosticsInSet( diag_set ); + if ( !num_child_diagnostics ) + return full_text; + + for ( uint i = 0; i < num_child_diagnostics; ++i ) { - X_VERIFY( unsaved_files[ i ].filename_ ); - X_VERIFY( unsaved_files[ i ].contents_ ); - X_VERIFY( unsaved_files[ i ].length_ ); - clang_unsaved_files[ i ].Filename = unsaved_files[ i ].filename_; - clang_unsaved_files[ i ].Contents = unsaved_files[ i ].contents_; - clang_unsaved_files[ i ].Length = unsaved_files[ i ].length_; + CXDiagnostic diagnostic = clang_getDiagnosticInSet( diag_set, i ); + if ( !diagnostic ) + continue; + + full_text.append( FullDiagnosticText( diagnostic ) ); } - return clang_unsaved_files; + return full_text; } -// 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 ) -{ - if ( !completion_string ) - return false; - return clang_getCompletionAvailability( completion_string ) == - CXAvailability_Available; -} - - - char DiagnosticSeverityToType( CXDiagnosticSeverity severity ) { switch ( severity ) @@ -81,6 +95,38 @@ char DiagnosticSeverityToType( CXDiagnosticSeverity severity ) } +// 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 ) +{ + if ( !completion_string ) + return false; + return clang_getCompletionAvailability( completion_string ) == + CXAvailability_Available; +} + +} // unnamed namespace + + +std::vector< CXUnsavedFile > ToCXUnsavedFiles( + const std::vector< UnsavedFile > &unsaved_files ) +{ + std::vector< CXUnsavedFile > clang_unsaved_files( unsaved_files.size() ); + for ( uint i = 0; i < unsaved_files.size(); ++i ) + { + X_VERIFY( unsaved_files[ i ].filename_ ); + X_VERIFY( unsaved_files[ i ].contents_ ); + X_VERIFY( unsaved_files[ i ].length_ ); + clang_unsaved_files[ i ].Filename = unsaved_files[ i ].filename_; + clang_unsaved_files[ i ].Contents = unsaved_files[ i ].contents_; + clang_unsaved_files[ i ].Length = unsaved_files[ i ].length_; + } + + return clang_unsaved_files; +} + + std::vector< CompletionData > ToCompletionDataVector( CXCodeCompleteResults *results ) { @@ -125,38 +171,6 @@ std::vector< CompletionData > ToCompletionDataVector( } -// NOTE: The passed in pointer should never be NULL! -// TODO: move all functions that are not external into an unnamed namespace -std::string FullDiagnosticText( CXDiagnostic cxdiagnostic ) -{ - std::string full_text = CXStringToString( clang_formatDiagnostic( - cxdiagnostic, - clang_defaultDiagnosticDisplayOptions() ) ); - - // 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( cxdiagnostic ); - if ( !diag_set ) - return full_text; - - uint num_child_diagnostics = clang_getNumDiagnosticsInSet( diag_set ); - if ( !num_child_diagnostics ) - return full_text; - - for ( uint i = 0; i < num_child_diagnostics; ++i ) - { - CXDiagnostic diagnostic = clang_getDiagnosticInSet( diag_set, i ); - if ( !diagnostic ) - continue; - - full_text.append( FullDiagnosticText( diagnostic ) ); - } - - return full_text; -} - - Diagnostic CXDiagnosticToDiagnostic( CXDiagnostic cxdiagnostic ) { Diagnostic diagnostic; diff --git a/cpp/ycm/ClangCompleter/ClangUtils.h b/cpp/ycm/ClangCompleter/ClangUtils.h index eb95cf91..914b0fe5 100644 --- a/cpp/ycm/ClangCompleter/ClangUtils.h +++ b/cpp/ycm/ClangCompleter/ClangUtils.h @@ -28,6 +28,8 @@ namespace YouCompleteMe { +std::string CXStringToString( CXString text ); + std::vector< CompletionData > ToCompletionDataVector( CXCodeCompleteResults *results ); diff --git a/cpp/ycm/ClangCompleter/CompletionData.cpp b/cpp/ycm/ClangCompleter/CompletionData.cpp index 7843eba3..3d3ae5b8 100644 --- a/cpp/ycm/ClangCompleter/CompletionData.cpp +++ b/cpp/ycm/ClangCompleter/CompletionData.cpp @@ -16,6 +16,7 @@ // along with YouCompleteMe. If not, see . #include "CompletionData.h" +#include "ClangUtils.h" #include #include @@ -145,18 +146,6 @@ std::string RemoveTwoConsecutiveUnderscores( std::string text ) namespace YouCompleteMe { -std::string CXStringToString( CXString text ) -{ - std::string final_string; - if ( !text.data ) - return final_string; - - final_string = std::string( clang_getCString( text ) ); - clang_disposeString( text ); - return final_string; -} - - CompletionData::CompletionData( const CXCompletionResult &completion_result ) { CXCompletionString completion_string = completion_result.CompletionString;