Skip to content

Instantly share code, notes, and snippets.

@bstaletic
Last active April 17, 2021 11:12
Show Gist options
  • Save bstaletic/18bde13731756a6c0ff4e7ead2a99a22 to your computer and use it in GitHub Desktop.
Save bstaletic/18bde13731756a6c0ff4e7ead2a99a22 to your computer and use it in GitHub Desktop.
diff --git a/cpp/ycm/CMakeLists.txt b/cpp/ycm/CMakeLists.txt
index a7d47bf7..168144cc 100644
--- a/cpp/ycm/CMakeLists.txt
+++ b/cpp/ycm/CMakeLists.txt
@@ -307,11 +307,27 @@ else()
message( FATAL_ERROR "Unknown compiler - C++17 filesystem library missing" )
endif()
+# Check if Abseil supports this environment
+file( WRITE ${CMAKE_CURRENT_BINARY_DIR}/main.cpp "#include <absl/base/policy_checks.h>\nint main() {}" )
+try_compile( ABSL_SUPPORTED ${CMAKE_CURRENT_BINARY_DIR}
+ SOURCES ${CMAKE_CURRENT_BINARY_DIR}/main.cpp
+ CMAKE_FLAGS INCLUDE_DIRECTORIES=${CMAKE_SOURCE_DIR}/absl
+ CXX_STANDARD 17
+ CXX_STANDARD_REQUIRED TRUE
+ CXX_EXTENSIONS FALSE )
+file( REMOVE ${CMAKE_CURRENT_BINARY_DIR}/main.cpp )
+
+if( ${ABSL_SUPPORTED} )
+ set( EXTRA_LIBS ${EXTRA_LIBS} absl::flat_hash_map absl::flat_hash_set )
+endif()
+
#############################################################################
-add_library( ${PROJECT_NAME} SHARED
- ${SERVER_SOURCES}
- )
+add_library( ${PROJECT_NAME} SHARED ${SERVER_SOURCES})
+
+if ( ${ABSL_SUPPORTED} )
+ target_compile_definitions( ${PROJECT_NAME} PRIVATE YCM_ABSEIL_SUPPORTED )
+endif()
# Makes MSVC conform to the standard
if ( MSVC )
@@ -338,8 +354,6 @@ target_link_libraries( ${PROJECT_NAME}
PUBLIC ${LIBCLANG_TARGET}
PUBLIC ${STD_FS_LIB}
PUBLIC ${EXTRA_LIBS}
- PUBLIC absl::flat_hash_map
- PUBLIC absl::flat_hash_set
)
if( LIBCLANG_TARGET )
diff --git a/cpp/ycm/IdentifierDatabase.cpp b/cpp/ycm/IdentifierDatabase.cpp
index cc6ddc24..9a289b9b 100644
--- a/cpp/ycm/IdentifierDatabase.cpp
+++ b/cpp/ycm/IdentifierDatabase.cpp
@@ -23,7 +23,22 @@
#include "Result.h"
#include "Utils.h"
+#ifdef YCM_ABSEIL_SUPPORTED
#include <absl/container/flat_hash_set.h>
+namespace YouCompleteMe {
+template< typename T, typename H, typename Eq >
+using HashSet = absl::flat_hash_set< T, H, Eq >;
+template< typename T > using Hash = absl::Hash< T >;
+} // namespace YouCompleteMe
+#else
+#include <unordered_set>
+namespace YouCompleteMe {
+template< typename T, typename H, typename Eq >
+using HashSet = std::unordered_set< T, H, Eq >;
+template< typename T >
+using Hash = std::hash< T >;
+} // namespace YouCompleteMe
+#endif
#include <memory>
namespace YouCompleteMe {
@@ -31,7 +46,7 @@ namespace YouCompleteMe {
namespace {
struct CandidateHasher {
size_t operator() ( const Candidate* c ) const noexcept {
- static absl::Hash< std::string > h;
+ static Hash< std::string > h;
return h(c->Text());
}
};
@@ -110,9 +125,9 @@ std::vector< Result > IdentifierDatabase::ResultsForQueryAndType(
}
Word query_object( std::move( query ) );
- absl::flat_hash_set< const Candidate *,
- CandidateHasher,
- CandidateCompareEq > seen_candidates;
+ HashSet< const Candidate *,
+ CandidateHasher,
+ CandidateCompareEq > seen_candidates;
seen_candidates.reserve( candidate_repository_.NumStoredElements() );
std::vector< Result > results;
diff --git a/cpp/ycm/IdentifierDatabase.h b/cpp/ycm/IdentifierDatabase.h
index d840a7df..d6644997 100644
--- a/cpp/ycm/IdentifierDatabase.h
+++ b/cpp/ycm/IdentifierDatabase.h
@@ -18,7 +18,19 @@
#ifndef IDENTIFIERDATABASE_H_ZESX3CVR
#define IDENTIFIERDATABASE_H_ZESX3CVR
+#ifdef YCM_ABSEIL_SUPPORTED
#include <absl/container/flat_hash_map.h>
+namespace YouCompleteMe {
+template< typename K, typename V >
+using HashMap = absl::flat_hash_map< K, V >;
+} // namespace YouCompleteMe
+#else
+#include <unordered_map>
+namespace YouCompleteMe {
+template< typename K, typename V >
+using HashMap = std::unordered_map< K, V >;
+} // namespace YouCompleteMe
+#endif
#include <memory>
#include <shared_mutex>
#include <string>
@@ -33,11 +45,10 @@ class Repository;
// filepath -> identifiers
-using FilepathToIdentifiers = absl::flat_hash_map< std::string,
- std::vector< std::string > >;
+using FilepathToIdentifiers = HashMap< std::string, std::vector< std::string > >;
// filetype -> (filepath -> identifiers)
-using FiletypeIdentifierMap = absl::flat_hash_map< std::string, FilepathToIdentifiers >;
+using FiletypeIdentifierMap = HashMap< std::string, FilepathToIdentifiers >;
// This class stores the database of identifiers the identifier completer has
diff --git a/cpp/ycm/Repository.h b/cpp/ycm/Repository.h
index cbe87bcc..4a6cd98a 100644
--- a/cpp/ycm/Repository.h
+++ b/cpp/ycm/Repository.h
@@ -23,7 +23,19 @@
#include "CodePoint.h"
#include "Utils.h"
+#ifdef YCM_ABSEIL_SUPPORTED
#include <absl/container/flat_hash_map.h>
+namespace YouCompleteMe {
+template< typename K, typename V >
+using HashMap = absl::flat_hash_map< K, V >;
+} // namespace YouCompleteMe
+#else
+#include <unordered_map>
+namespace YouCompleteMe {
+template< typename K, typename V >
+using HashMap = std::unordered_map< K, V >;
+} // namespace YouCompleteMe
+#endif
#include <memory>
#include <shared_mutex>
#include <string>
@@ -38,7 +50,7 @@ namespace YouCompleteMe {
template< typename T >
class Repository {
public:
- using Holder = absl::flat_hash_map< std::string, std::unique_ptr< T > >;
+ using Holder = HashMap< std::string, std::unique_ptr< T > >;
using Sequence = std::vector< const T* >;
static Repository &Instance() {
static Repository repo;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment