Skip to content

Commit

Permalink
Fix the SQLITECPP_PURE_FUNC macro to actually use the correct "pure" …
Browse files Browse the repository at this point in the history
…attribute
  • Loading branch information
SRombauts committed Jan 14, 2020
1 parent 13759e1 commit ffc1a3e
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 26 deletions.
27 changes: 1 addition & 26 deletions include/SQLiteCpp/Statement.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,37 +11,12 @@
#pragma once

#include <SQLiteCpp/Exception.h>
#include <SQLiteCpp/Utils.h> // SQLITECPP_PURE_FUNC

#include <string>
#include <map>
#include <climits> // For INT_MAX

// some macros are taken from https://github.com/nemequ/hedley/blob/master/hedley.h , it was public domain that time
#if defined(__GNUC__) || defined(__GNUG__) || defined(__clang__) ||\
(defined(__INTEL_COMPILER) && __INTEL_COMPILER > 1600) ||\
(defined(__ARMCC_VERSION) && __ARMCC_VERSION > 4010000) ||\
(\
defined(__TI_COMPILER_VERSION__) && (\
__TI_COMPILER_VERSION__ > 8003000 ||\
(__TI_COMPILER_VERSION__ > 7003000 && defined(__TI_GNU_ATTRIBUTE_SUPPORT__))\
)\
)
#if defined(__has_attribute)
#if !defined(SQLITECPP_PURE_FUNC) && __has_attribute(const)
#define SQLITECPP_PURE_FUNC __attribute__((const))
#endif
#endif
#endif
#if !defined(SQLITECPP_PURE_FUNC)
#define SQLITECPP_PURE_FUNC
#if defined(__GNUC__) || defined(__GNUG__) || defined(__clang__)
#warning "You have a compiler without the needed macros or attributes. Cannot detect and apply support of pure const functions. Efficiency likely will be harmed. Consider using another compiler or its version"
#elif _MSC_VER
#pragma message "You have a compiler without the needed macros or attributes. Cannot detect and apply support of pure const functions. Efficiency likely will be harmed. Consider using another compiler or its version"
#endif
#endif


// Forward declarations to avoid inclusion of <sqlite3.h> in a header
struct sqlite3;
struct sqlite3_stmt;
Expand Down
31 changes: 31 additions & 0 deletions include/SQLiteCpp/Utils.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
/**
* @file Utils.h
* @ingroup SQLiteCpp
* @brief Definition of the SQLITECPP_PURE_FUNC macro.
*
* Copyright (c) 2012-2020 Sebastien Rombauts (sebastien.rombauts@gmail.com)
*
* Distributed under the MIT License (MIT) (See accompanying file LICENSE.txt
* or copy at http://opensource.org/licenses/MIT)
*/
#pragma once

// macro taken from https://github.com/nemequ/hedley/blob/master/hedley.h that was in public domain at this time
#if defined(__GNUC__) || defined(__GNUG__) || defined(__clang__) ||\
(defined(__INTEL_COMPILER) && __INTEL_COMPILER > 1600) ||\
(defined(__ARMCC_VERSION) && __ARMCC_VERSION > 4010000) ||\
(\
defined(__TI_COMPILER_VERSION__) && (\
__TI_COMPILER_VERSION__ > 8003000 ||\
(__TI_COMPILER_VERSION__ > 7003000 && defined(__TI_GNU_ATTRIBUTE_SUPPORT__))\
)\
)
#if defined(__has_attribute)
#if !defined(SQLITECPP_PURE_FUNC) && __has_attribute(pure)
#define SQLITECPP_PURE_FUNC __attribute__((pure))

This comment has been minimized.

Copy link
@KOLANICH

KOLANICH Jan 14, 2020

Contributor

No, it should be const. I have done some experiments (not on SQLiteCPP, but on some synthetic example that checked how many times the stuff was called when called twice with 2 identical C-strings (which must be merged into 1 single pointer when the optimizer is enabled, and since the func is "pure" the second call is eliminated reusing the results from the first call) ), only when using const it was working as intended (a single call to the "pure" function) and in fact there was an article in the net decribing their differences.

#endif
#endif
#endif
#if !defined(SQLITECPP_PURE_FUNC)
#define SQLITECPP_PURE_FUNC
#endif

0 comments on commit ffc1a3e

Please sign in to comment.