r316264 - [bindings] allow null strings in Python 3
Author: frutiger Date: Sat Oct 21 09:13:41 2017 New Revision: 316264 URL: http://llvm.org/viewvc/llvm-project?rev=316264&view=rev Log: [bindings] allow null strings in Python 3 Some API calls accept 'NULL' instead of a char array (e.g. the second argument to 'clang_ParseTranslationUnit'). For Python 3 compatibility, all strings are passed through 'c_interop_string' which expects to receive only 'bytes' or 'str' objects. This change extends this behavior to additionally allow 'None' to be supplied. A test case was added which breaks in Python 3, and is fixed by this change. All the test cases pass in both, Python 2 and Python 3. Modified: cfe/trunk/bindings/python/clang/cindex.py cfe/trunk/bindings/python/tests/cindex/test_index.py Modified: cfe/trunk/bindings/python/clang/cindex.py URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/bindings/python/clang/cindex.py?rev=316264&r1=316263&r2=316264&view=diff == --- cfe/trunk/bindings/python/clang/cindex.py (original) +++ cfe/trunk/bindings/python/clang/cindex.py Sat Oct 21 09:13:41 2017 @@ -94,6 +94,9 @@ if sys.version_info[0] == 3: return cls(param) if isinstance(param, bytes): return cls(param) +if param is None: +# Support passing null to C functions expecting char arrays +return None raise TypeError("Cannot convert '{}' to '{}'".format(type(param).__name__, cls.__name__)) @staticmethod Modified: cfe/trunk/bindings/python/tests/cindex/test_index.py URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/bindings/python/tests/cindex/test_index.py?rev=316264&r1=316263&r2=316264&view=diff == --- cfe/trunk/bindings/python/tests/cindex/test_index.py (original) +++ cfe/trunk/bindings/python/tests/cindex/test_index.py Sat Oct 21 09:13:41 2017 @@ -13,3 +13,5 @@ def test_parse(): assert isinstance(index, Index) tu = index.parse(os.path.join(kInputsDir, 'hello.cpp')) assert isinstance(tu, TranslationUnit) +tu = index.parse(None, ['-c', os.path.join(kInputsDir, 'hello.cpp')]) +assert isinstance(tu, TranslationUnit) ___ cfe-commits mailing list cfe-commits@lists.llvm.org http://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
r316263 - Test commit
Author: frutiger Date: Sat Oct 21 09:03:17 2017 New Revision: 316263 URL: http://llvm.org/viewvc/llvm-project?rev=316263&view=rev Log: Test commit Modified: cfe/trunk/README.txt Modified: cfe/trunk/README.txt URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/README.txt?rev=316263&r1=316262&r2=316263&view=diff == --- cfe/trunk/README.txt (original) +++ cfe/trunk/README.txt Sat Oct 21 09:03:17 2017 @@ -13,10 +13,10 @@ different source-level tools. One examp If you're interested in more (including how to build Clang) it is best to read the relevant web sites. Here are some pointers: -Information on Clang: http://clang.llvm.org/ -Building and using Clang: http://clang.llvm.org/get_started.html -Clang Static Analyzer: http://clang-analyzer.llvm.org/ -Information on the LLVM project: http://llvm.org/ +Information on Clang: http://clang.llvm.org/ +Building and using Clang: http://clang.llvm.org/get_started.html +Clang Static Analyzer:http://clang-analyzer.llvm.org/ +Information on the LLVM project: http://llvm.org/ If you have questions or comments about Clang, a great place to discuss them is on the Clang development mailing list: ___ cfe-commits mailing list cfe-commits@lists.llvm.org http://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[PATCH] [bindings] fix TLS test failure
Since cfe commit r237337, '__declspec(thread)' and 'thread_local' have been the same since MSVC 2015. i.e. they are both considered to supply a dynamic TLS kind, not a static TLS kind. This test originally did not specify which version of MS compatibility to assume. As a result, the test was brittle, since changing the default compatibility version could break the test. This commit adds a specific version when building up the flags used to parse the translation unit, and tests both versions. --- bindings/python/tests/cindex/test_tls_kind.py | 18 ++ 1 file changed, 14 insertions(+), 4 deletions(-) diff --git a/bindings/python/tests/cindex/test_tls_kind.py b/bindings/python/tests/cindex/test_tls_kind.py index 6a03c0d5ee..d0ee4587bc 100644 --- a/bindings/python/tests/cindex/test_tls_kind.py +++ b/bindings/python/tests/cindex/test_tls_kind.py @@ -27,11 +27,21 @@ _Thread_local int tls_static; # The following case tests '__declspec(thread)'. Since it is a Microsoft # specific extension, specific flags are required for the parser to pick # these up. -flags = ['-fms-extensions', '-target', 'x86_64-unknown-windows-win32'] +flags = ['-fms-extensions', '-target', 'x86_64-unknown-windows-win32', + '-fms-compatibility-version=18'] tu = get_tu(""" -__declspec(thread) int tls_declspec; +__declspec(thread) int tls_declspec_msvc18; """, lang = 'cpp', flags=flags) -tls_declspec = get_cursor(tu.cursor, 'tls_declspec') -assert tls_declspec.tls_kind == TLSKind.STATIC +tls_declspec_msvc18 = get_cursor(tu.cursor, 'tls_declspec_msvc18') +assert tls_declspec_msvc18.tls_kind == TLSKind.STATIC + +flags = ['-fms-extensions', '-target', 'x86_64-unknown-windows-win32', + '-fms-compatibility-version=19'] +tu = get_tu(""" +__declspec(thread) int tls_declspec_msvc19; +""", lang = 'cpp', flags=flags) + +tls_declspec_msvc19 = get_cursor(tu.cursor, 'tls_declspec_msvc19') +assert tls_declspec_msvc19.tls_kind == TLSKind.DYNAMIC ___ cfe-commits mailing list cfe-commits@lists.llvm.org http://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
r316278 - [libclang, bindings]: add spelling location
Author: frutiger Date: Sat Oct 21 13:53:49 2017 New Revision: 316278 URL: http://llvm.org/viewvc/llvm-project?rev=316278&view=rev Log: [libclang, bindings]: add spelling location o) Add a 'Location' class that represents the four properties of a physical location o) Enhance 'SourceLocation' to provide 'expansion' and 'spelling' locations, maintaining backwards compatibility with existing code by forwarding the four properties to 'expansion'. o) Update the implementation to use 'clang_getExpansionLocation' instead of the deprecated 'clang_getInstantiationLocation', which has been present since 2011. o) Update the implementation of 'clang_getSpellingLocation' to actually obtain spelling location instead of file location. Modified: cfe/trunk/bindings/python/clang/cindex.py cfe/trunk/bindings/python/tests/cindex/test_location.py cfe/trunk/tools/libclang/CXSourceLocation.cpp Modified: cfe/trunk/bindings/python/clang/cindex.py URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/bindings/python/clang/cindex.py?rev=316278&r1=316277&r2=316278&view=diff == --- cfe/trunk/bindings/python/clang/cindex.py (original) +++ cfe/trunk/bindings/python/clang/cindex.py Sat Oct 21 13:53:49 2017 @@ -214,25 +214,45 @@ class _CXString(Structure): assert isinstance(res, _CXString) return conf.lib.clang_getCString(res) +class Location(object): +"""A Location is a specific kind of source location. A SourceLocation +refers to several kinds of locations (e.g. spelling location vs. expansion +location).""" + +def __init__(self, file, line, column, offset): +self._file = File(file) if file else None +self._line = int(line.value) +self._column = int(column.value) +self._offset = int(offset.value) + + +@property +def file(self): +"""Get the file represented by this source location.""" +return self._file + +@property +def line(self): +"""Get the line represented by this source location.""" +return self._line + +@property +def column(self): +"""Get the column represented by this source location.""" +return self._column + +@property +def offset(self): +"""Get the file offset represented by this source location.""" +return self._offset class SourceLocation(Structure): """ A SourceLocation represents a particular location within a source file. """ _fields_ = [("ptr_data", c_void_p * 2), ("int_data", c_uint)] -_data = None - -def _get_instantiation(self): -if self._data is None: -f, l, c, o = c_object_p(), c_uint(), c_uint(), c_uint() -conf.lib.clang_getInstantiationLocation(self, byref(f), byref(l), -byref(c), byref(o)) -if f: -f = File(f) -else: -f = None -self._data = (f, int(l.value), int(c.value), int(o.value)) -return self._data +_expansion = None +_spelling = None @staticmethod def from_position(tu, file, line, column): @@ -253,24 +273,72 @@ class SourceLocation(Structure): return conf.lib.clang_getLocationForOffset(tu, file, offset) @property +def expansion(self): +""" +The source location where then entity this object is referring to is +expanded. +""" +if not self._expansion: +file = c_object_p() +line = c_uint() +column = c_uint() +offset = c_uint() +conf.lib.clang_getExpansionLocation(self, +byref(file), +byref(line), +byref(column), +byref(offset)) + +self._expansion = Location(file, line, column, offset) +return self._expansion + +@property +def spelling(self): +""" +The source location where then entity this object is referring to is +written. +""" +if not self._spelling: +file = c_object_p() +line = c_uint() +column = c_uint() +offset = c_uint() +conf.lib.clang_getSpellingLocation(self, + byref(file), + byref(line), + byref(column), + byref(offset)) + +self._spelling = Location(file, line, column, offset) +return self._spelling + +@property def file(self): -"""Get the file represented by this source location.""" -return self._get_instantiation()[0] +"""Get the file represented by this source loca
Re: r316278 - [libclang, bindings]: add spelling location
Thanks, I will take a look. On Sat, Oct 21, 2017 at 5:53 PM, Aaron Ballman wrote: > I've reverted back to green in r316279 due to more bots failing. > > ~Aaron > > On Sat, Oct 21, 2017 at 5:35 PM, Aaron Ballman > wrote: > > This commit appears to have broken several bots. Can you revert or > > quickly fix the issue? > > > > http://lab.llvm.org:8011/builders/clang-ppc64be-linux/builds/11896 > > http://lab.llvm.org:8011/builders/clang-s390x-linux/builds/12380 > > > > Thanks! > > > > ~Aaron > > > > On Sat, Oct 21, 2017 at 4:53 PM, Masud Rahman via cfe-commits > > wrote: > >> Author: frutiger > >> Date: Sat Oct 21 13:53:49 2017 > >> New Revision: 316278 > >> > >> URL: http://llvm.org/viewvc/llvm-project?rev=316278&view=rev > >> Log: > >> [libclang, bindings]: add spelling location > >> > >> o) Add a 'Location' class that represents the four properties of a > >> physical location > >> > >> o) Enhance 'SourceLocation' to provide 'expansion' and 'spelling' > >> locations, maintaining backwards compatibility with existing code by > >> forwarding the four properties to 'expansion'. > >> > >> o) Update the implementation to use 'clang_getExpansionLocation' > >> instead of the deprecated 'clang_getInstantiationLocation', which > >> has been present since 2011. > >> > >> o) Update the implementation of 'clang_getSpellingLocation' to actually > >> obtain spelling location instead of file location. > >> > >> Modified: > >> cfe/trunk/bindings/python/clang/cindex.py > >> cfe/trunk/bindings/python/tests/cindex/test_location.py > >> cfe/trunk/tools/libclang/CXSourceLocation.cpp > >> > >> Modified: cfe/trunk/bindings/python/clang/cindex.py > >> URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/bindings/ > python/clang/cindex.py?rev=316278&r1=316277&r2=316278&view=diff > >> > == > >> --- cfe/trunk/bindings/python/clang/cindex.py (original) > >> +++ cfe/trunk/bindings/python/clang/cindex.py Sat Oct 21 13:53:49 2017 > >> @@ -214,25 +214,45 @@ class _CXString(Structure): > >> assert isinstance(res, _CXString) > >> return conf.lib.clang_getCString(res) > >> > >> +class Location(object): > >> +"""A Location is a specific kind of source location. A > SourceLocation > >> +refers to several kinds of locations (e.g. spelling location vs. > expansion > >> +location).""" > >> + > >> +def __init__(self, file, line, column, offset): > >> +self._file = File(file) if file else None > >> +self._line = int(line.value) > >> +self._column = int(column.value) > >> +self._offset = int(offset.value) > >> + > >> + > >> +@property > >> +def file(self): > >> +"""Get the file represented by this source location.""" > >> +return self._file > >> + > >> +@property > >> +def line(self): > >> +"""Get the line represented by this source location.""" > >> +return self._line > >> + > >> +@property > >> +def column(self): > >> +"""Get the column represented by this source location.""" > >> +return self._column > >> + > >> +@property > >> +def offset(self): > >> +"""Get the file offset represented by this source location.""" > >> +return self._offset > >> > >> class SourceLocation(Structure): > >> """ > >> A SourceLocation represents a particular location within a source > file. > >> """ > >> _fields_ = [("ptr_data", c_void_p * 2), ("int_data", c_uint)] > >> -_data = None > >> - > >> -def _get_instantiation(self): > >> -if self._data is None: > >> -f, l, c, o = c_object_p(), c_uint(), c_uint(), c_uint() > >> -conf.lib.clang_getInstantiationLocation(self, byref(f), > byref(l)
Re: r316278 - [libclang, bindings]: add spelling location
I created a new revision to review and to fix the tests: https://reviews.llvm.org/D39217. On Sat, Oct 21, 2017 at 6:24 PM, Masud Rahman wrote: > Thanks, I will take a look. > > On Sat, Oct 21, 2017 at 5:53 PM, Aaron Ballman > wrote: > >> I've reverted back to green in r316279 due to more bots failing. >> >> ~Aaron >> >> On Sat, Oct 21, 2017 at 5:35 PM, Aaron Ballman >> wrote: >> > This commit appears to have broken several bots. Can you revert or >> > quickly fix the issue? >> > >> > http://lab.llvm.org:8011/builders/clang-ppc64be-linux/builds/11896 >> > http://lab.llvm.org:8011/builders/clang-s390x-linux/builds/12380 >> > >> > Thanks! >> > >> > ~Aaron >> > >> > On Sat, Oct 21, 2017 at 4:53 PM, Masud Rahman via cfe-commits >> > wrote: >> >> Author: frutiger >> >> Date: Sat Oct 21 13:53:49 2017 >> >> New Revision: 316278 >> >> >> >> URL: http://llvm.org/viewvc/llvm-project?rev=316278&view=rev >> >> Log: >> >> [libclang, bindings]: add spelling location >> >> >> >> o) Add a 'Location' class that represents the four properties of a >> >> physical location >> >> >> >> o) Enhance 'SourceLocation' to provide 'expansion' and 'spelling' >> >> locations, maintaining backwards compatibility with existing code >> by >> >> forwarding the four properties to 'expansion'. >> >> >> >> o) Update the implementation to use 'clang_getExpansionLocation' >> >> instead of the deprecated 'clang_getInstantiationLocation', which >> >> has been present since 2011. >> >> >> >> o) Update the implementation of 'clang_getSpellingLocation' to >> actually >> >> obtain spelling location instead of file location. >> >> >> >> Modified: >> >> cfe/trunk/bindings/python/clang/cindex.py >> >> cfe/trunk/bindings/python/tests/cindex/test_location.py >> >> cfe/trunk/tools/libclang/CXSourceLocation.cpp >> >> >> >> Modified: cfe/trunk/bindings/python/clang/cindex.py >> >> URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/bindings/pytho >> n/clang/cindex.py?rev=316278&r1=316277&r2=316278&view=diff >> >> >> == >> >> --- cfe/trunk/bindings/python/clang/cindex.py (original) >> >> +++ cfe/trunk/bindings/python/clang/cindex.py Sat Oct 21 13:53:49 2017 >> >> @@ -214,25 +214,45 @@ class _CXString(Structure): >> >> assert isinstance(res, _CXString) >> >> return conf.lib.clang_getCString(res) >> >> >> >> +class Location(object): >> >> +"""A Location is a specific kind of source location. A >> SourceLocation >> >> +refers to several kinds of locations (e.g. spelling location vs. >> expansion >> >> +location).""" >> >> + >> >> +def __init__(self, file, line, column, offset): >> >> +self._file = File(file) if file else None >> >> +self._line = int(line.value) >> >> +self._column = int(column.value) >> >> +self._offset = int(offset.value) >> >> + >> >> + >> >> +@property >> >> +def file(self): >> >> +"""Get the file represented by this source location.""" >> >> +return self._file >> >> + >> >> +@property >> >> +def line(self): >> >> +"""Get the line represented by this source location.""" >> >> +return self._line >> >> + >> >> +@property >> >> +def column(self): >> >> +"""Get the column represented by this source location.""" >> >> +return self._column >> >> + >> >> +@property >> >> +def offset(self): >> >> +"""Get the file offset represented by this source location.""" >> >> +return self._offset >> >> >> >> class SourceLocation(Structure): >> >> """ >> >> A
[bindings] add Cursor.linkage
commit cc52d367ede0f3f306014d0418e18772e0c49dec Author: Masud Rahman Date: Thu Sep 7 11:39:19 2017 -0400 [bindings] add Cursor.linkage Add Python bindings for the 'clang_getCursorLinkage', and tests to validate the functionality. diff --git a/bindings/python/clang/cindex.py b/bindings/python/clang/cindex.py index 4069ab8650..5713ab7f29 100644 --- a/bindings/python/clang/cindex.py +++ b/bindings/python/clang/cindex.py @@ -1548,6 +1548,13 @@ class Cursor(Structure): return self._loc +@property +def linkage(self): +if not hasattr(self, '_linkage'): +self._linkage = conf.lib.clang_getCursorLinkage(self) + +return LinkageKind.from_id(self._linkage) + @property def extent(self): """ @@ -2061,6 +2068,26 @@ RefQualifierKind.NONE = RefQualifierKind(0) RefQualifierKind.LVALUE = RefQualifierKind(1) RefQualifierKind.RVALUE = RefQualifierKind(2) +class LinkageKind(BaseEnumeration): +""" +""" + +# The unique kind objects, indexed by id. +_kinds = [] +_name_map = None + +def from_param(self): +return self.value + +def __repr__(self): +return 'LinkageKind.%s' % (self.name,) + +LinkageKind.INVALID = LinkageKind(0) +LinkageKind.NO_LINKAGE = LinkageKind(1) +LinkageKind.INTERNAL = LinkageKind(2) +LinkageKind.UNIQUE_EXTERNAL = LinkageKind(3) +LinkageKind.EXTERNAL = LinkageKind(4) + class Type(Structure): """ The type of an element in the abstract syntax tree. diff --git a/bindings/python/tests/cindex/test_linkage.py b/bindings/python/tests/cindex/test_linkage.py new file mode 100644 index 00..392a0f156c --- /dev/null +++ b/bindings/python/tests/cindex/test_linkage.py @@ -0,0 +1,31 @@ + +from clang.cindex import LinkageKind +from clang.cindex import Cursor +from clang.cindex import TranslationUnit + +from .util import get_cursor +from .util import get_tu + +def test_linkage(): +"""Ensure that linkage specifers are available on cursors""" + +tu = get_tu(""" +void foo() { int no_linkage; } +static int internal; +namespace { extern int unique_external; } +extern int external; +""", lang = 'cpp') + +no_linkage = get_cursor(tu.cursor, "no_linkage") +assert no_linkage.linkage == LinkageKind.NO_LINKAGE; + +internal = get_cursor(tu.cursor, "internal") +assert internal.linkage == LinkageKind.INTERNAL + +unique_external = get_cursor(tu.cursor, "unique_external") +assert unique_external.linkage == LinkageKind.UNIQUE_EXTERNAL + +external = get_cursor(tu.cursor, "external") +assert external.linkage == LinkageKind.EXTERNAL + + ___ cfe-commits mailing list cfe-commits@lists.llvm.org http://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
r317706 - [bindings] fix TLS test failure
Author: frutiger Date: Wed Nov 8 11:17:27 2017 New Revision: 317706 URL: http://llvm.org/viewvc/llvm-project?rev=317706&view=rev Log: [bindings] fix TLS test failure Since cfe commit r237337, '__declspec(thread)' and 'thread_local' have been the same since MSVC 2015. i.e. they are both considered to supply a dynamic TLS kind, not a static TLS kind. This test originally did not specify which version of MS compatibility to assume. As a result, the test was brittle, since changing the default compatibility version could break the test. This commit adds a specific version when building up the flags used to parse the translation unit, and tests both versions. Modified: cfe/trunk/bindings/python/tests/cindex/test_tls_kind.py Modified: cfe/trunk/bindings/python/tests/cindex/test_tls_kind.py URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/bindings/python/tests/cindex/test_tls_kind.py?rev=317706&r1=317705&r2=317706&view=diff == --- cfe/trunk/bindings/python/tests/cindex/test_tls_kind.py (original) +++ cfe/trunk/bindings/python/tests/cindex/test_tls_kind.py Wed Nov 8 11:17:27 2017 @@ -27,11 +27,21 @@ _Thread_local int tls_static; # The following case tests '__declspec(thread)'. Since it is a Microsoft # specific extension, specific flags are required for the parser to pick # these up. -flags = ['-fms-extensions', '-target', 'x86_64-unknown-windows-win32'] +flags = ['-fms-extensions', '-target', 'x86_64-unknown-windows-win32', + '-fms-compatibility-version=18'] tu = get_tu(""" -__declspec(thread) int tls_declspec; +__declspec(thread) int tls_declspec_msvc18; """, lang = 'cpp', flags=flags) -tls_declspec = get_cursor(tu.cursor, 'tls_declspec') -assert tls_declspec.tls_kind == TLSKind.STATIC +tls_declspec_msvc18 = get_cursor(tu.cursor, 'tls_declspec_msvc18') +assert tls_declspec_msvc18.tls_kind == TLSKind.STATIC + +flags = ['-fms-extensions', '-target', 'x86_64-unknown-windows-win32', + '-fms-compatibility-version=19'] +tu = get_tu(""" +__declspec(thread) int tls_declspec_msvc19; +""", lang = 'cpp', flags=flags) + +tls_declspec_msvc19 = get_cursor(tu.cursor, 'tls_declspec_msvc19') +assert tls_declspec_msvc19.tls_kind == TLSKind.DYNAMIC ___ cfe-commits mailing list cfe-commits@lists.llvm.org http://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[PATCH] D16967: support/allocators: implements requirements
frutiger created this revision. frutiger added reviewers: mclow.lists, EricWF. frutiger added a subscriber: cfe-commits. This commit adds the required constructor overloads and member functions to make the supporting allocators actually valid allocators. The implementation of 'allocate' defers to 'std::malloc', and correspondingly that of 'deallocate' defers to 'std::free'. This commit also changes supporting allocators 'A1' to track the number of elements requested and 'A2' to track the hint that was requested in order to facilitate testing. Two (seemingly unrelated) tests seem to fail: Failing Tests (2): libc++ :: std/utilities/tuple/tuple.general/tuple.smartptr.pass.cpp libc++ :: std/utilities/tuple/tuple.tuple/tuple.creation/tuple_cat.pass.cpp http://reviews.llvm.org/D16967 Files: test/std/utilities/allocator.adaptor/allocator.adaptor.members/allocate_size.pass.cpp test/std/utilities/allocator.adaptor/allocator.adaptor.members/allocate_size_hint.pass.cpp test/std/utilities/allocator.adaptor/allocator.adaptor.members/deallocate.pass.cpp test/support/allocators.h Index: test/support/allocators.h === --- test/support/allocators.h +++ test/support/allocators.h @@ -10,6 +10,7 @@ #ifndef ALLOCATORS_H #define ALLOCATORS_H +#include #include #include @@ -30,7 +31,7 @@ static bool copy_called; static bool move_called; -static bool allocate_called; +static std::size_t allocate_called; static std::pair deallocate_called; A1(const A1& a) TEST_NOEXCEPT : id_(a.id()) {copy_called = true;} @@ -45,12 +46,13 @@ T* allocate(std::size_t n) { -allocate_called = true; -return (T*)n; +allocate_called = n; +return static_cast(std::malloc(sizeof(T) * n)); } void deallocate(T* p, std::size_t n) { +std::free(p); deallocate_called = std::pair(p, n); } @@ -59,7 +61,7 @@ template bool A1::copy_called = false; template bool A1::move_called = false; -template bool A1::allocate_called = false; +template std::size_t A1::allocate_called = 0; template std::pair A1::deallocate_called; template @@ -94,23 +96,36 @@ static bool copy_called; static bool move_called; -static bool allocate_called; +static const void *allocate_called; A2(const A2& a) TEST_NOEXCEPT : id_(a.id()) {copy_called = true;} A2(A2&& a) TEST_NOEXCEPT : id_(a.id()) {move_called = true;} A2& operator=(const A2& a) TEST_NOEXCEPT { id_ = a.id(); copy_called = true; return *this;} A2& operator=(A2&& a) TEST_NOEXCEPT { id_ = a.id(); move_called = true; return *this;} +template +A2(const A2& a) TEST_NOEXCEPT : id_(a.id()) {copy_called = true;} + +T* allocate(std::size_t n) +{ +return static_cast(std::malloc(sizeof(T) * n)); +} + T* allocate(std::size_t n, const void* hint) { -allocate_called = true; -return (T*)hint; +allocate_called = hint; +return static_cast(std::malloc(sizeof(T) * n)); +} + +void deallocate(T* p, std::size_t n) +{ +std::free(p); } }; template bool A2::copy_called = false; template bool A2::move_called = false; -template bool A2::allocate_called = false; +template const void *A2::allocate_called = (const void*)0; template inline @@ -150,6 +165,19 @@ A3& operator=(const A3& a) TEST_NOEXCEPT { id_ = a.id(); copy_called = true; return *this;} A3& operator=(A3&& a) TEST_NOEXCEPT { id_ = a.id(); move_called = true; return *this;} +template +A3(const A3& a) TEST_NOEXCEPT : id_(a.id()) {copy_called = true;} + +T* allocate(std::size_t n) +{ +return static_cast(std::malloc(sizeof(T) * n)); +} + +void deallocate(T* p, std::size_t n) +{ +std::free(p); +} + template void construct(U* p, Args&& ...args) { Index: test/std/utilities/allocator.adaptor/allocator.adaptor.members/deallocate.pass.cpp === --- test/std/utilities/allocator.adaptor/allocator.adaptor.members/deallocate.pass.cpp +++ test/std/utilities/allocator.adaptor/allocator.adaptor.members/deallocate.pass.cpp @@ -26,20 +26,23 @@ { typedef std::scoped_allocator_adaptor> A; A a; -a.deallocate((int*)10, 20); -assert((A1::deallocate_called == std::pair((int*)10, 20))); +int* p = a.allocate(20); +a.deallocate(p, 20); +assert((A1::deallocate_called == std::pair(p, 20))); } { typedef std::scoped_allocator_adaptor, A2> A; A a; -a.deallocate((int*)10, 20); -assert((A1::deallocate_called == std::pair((int*)10, 20))); +int* p = a.allocate(20); +a.deallocate(p, 20); +assert((A1::deallocate_called == std::pair(p, 20))); } { typedef std::
Re: [PATCH] D16970: scoped alloc construct: adds basic tests
frutiger updated this revision to Diff 47141. frutiger added a comment. Added `operator==` and `operator!=`. http://reviews.llvm.org/D16970 Files: test/std/utilities/allocator.adaptor/scoped.adaptor.operators/construct.pass.cpp test/support/instrumentingallocators.h Index: test/support/instrumentingallocators.h === --- /dev/null +++ test/support/instrumentingallocators.h @@ -0,0 +1,75 @@ +//===--===// +// +// The LLVM Compiler Infrastructure +// +// This file is dual licensed under the MIT and the University of Illinois Open +// Source Licenses. See LICENSE.TXT for details. +// +//===--===// + +#ifndef ALLOCATORS_H +#define ALLOCATORS_H + +#include +#include + +struct Instrumentation { +typedef std::unordered_map Allocations; + +Allocations allocs_; +size_t numAllocs_= 0; +size_t numDeallocs_ = 0; +size_t numAllocsLastChecked_ = 0; + +~Instrumentation() { +assert(allocs_.empty()); +} + +void checkAllocsIncreased() { +assert(numAllocs_ > numAllocsLastChecked_); +numAllocsLastChecked_ = numAllocs_; +} +}; + +template +struct IA1 { +Instrumentation *instrumentation_; + +typedef T value_type; + +explicit IA1(Instrumentation *instrumentation) : +instrumentation_(instrumentation) {} + +template +IA1(const IA1& other) : instrumentation_(other.instrumentation_) {} + +T *allocate(size_t n) { +void *result = std::malloc(sizeof(T) * n); +assert(instrumentation_->allocs_.find(result) == + instrumentation_->allocs_.end()); +instrumentation_->allocs_[result] = n; +++instrumentation_->numAllocs_; +return static_cast(result); +} + +void deallocate(T *ptr, size_t n) { +auto alloc = instrumentation_->allocs_.find(ptr); +assert(alloc != instrumentation_->allocs_.end()); +assert(alloc->second == n); +instrumentation_->allocs_.erase(alloc); +++instrumentation_->numDeallocs_; +std::free(ptr); +} +}; + +template +bool operator==(const IA1& lhs, const IA1& rhs) { +return lhs.instrumentation_ == rhs.instrumentation_; +} + +template +bool operator!=(const IA1& lhs, const IA1& rhs) { +return !(lhs == rhs); +} + +#endif Index: test/std/utilities/allocator.adaptor/scoped.adaptor.operators/construct.pass.cpp === --- /dev/null +++ test/std/utilities/allocator.adaptor/scoped.adaptor.operators/construct.pass.cpp @@ -0,0 +1,82 @@ +//===--===// +// +// The LLVM Compiler Infrastructure +// +// This file is dual licensed under the MIT and the University of Illinois Open +// Source Licenses. See LICENSE.TXT for details. +// +//===--===// + +// + +// template +// class scoped_allocator_adaptor + +// template void construct(T* p, Args&& args); +// template +// void construct(pair* p, piecewise_construct t, tuple x, +//tuple y); +// template +// void construct(pair* p); +// template +// void construct(pair* p, U&& x, V&& y); +// template +// void construct(pair* p, const pair& x); +// template +// void construct(pair* p, pair&& x); + +#include +#include +#include +#include + +#include "instrumentingallocators.h" + +namespace scoped { + +template +using IA1 = std::scoped_allocator_adaptor>; + +template +using List1 = std::list>; + +} + +int main() +{ +Instrumentation instrumentation; +{ +typedef scoped::List1 List; + +List::allocator_type alloc(&instrumentation); +List list(alloc); +list.emplace_back(); +instrumentation.checkAllocsIncreased(); +list.emplace_back(list.back()); +instrumentation.checkAllocsIncreased(); +} +assert(instrumentation.allocs_.size() == 0); +{ +typedef scoped::List1> List; + +List::allocator_type alloc(&instrumentation); +List list(alloc); +list.emplace_back(); +instrumentation.checkAllocsIncreased(); +list.emplace_back(list.back()); +instrumentation.checkAllocsIncreased(); +} +assert(instrumentation.allocs_.size() == 0); +{ +typedef scoped::List1> List; + +List::allocator_type alloc(&instrumentation); +List list(alloc); +list.emplace_back(); +instrumentation.checkAllocsIncreased(); +list.emplace_back(list.back()); +instrumentation.checkAllocsIncreased(); +} +assert(instrumentation.allocs_.size() == 0); +} + ___ cfe-commits maili
[PATCH] D16970: scoped alloc construct: adds basic tests
frutiger created this revision. frutiger added reviewers: EricWF, mclow.lists. frutiger added a subscriber: cfe-commits. This commit introduces a new supporting allocator 'IA1' in 'support/instrumentingallocators.h' which tracks allocations and deallocations. This commit also introduces a new test driver 'construct.pass.cpp' which tests using the 'scoped_allocator_adaptor' with three types which take allocators in different ways: 1. container-style with the allocator at the end 2. std::function-style with the allocator at the start 3. allocator-unaware types This will provide a starting point for scenarios where argument lists need to be adapted before being forwarded for 'pair' where 'T1' and 'T2' may be combinations of the above three classes of types. http://reviews.llvm.org/D16970 Files: test/std/utilities/allocator.adaptor/scoped.adaptor.operators/construct.pass.cpp test/support/instrumentingallocators.h Index: test/support/instrumentingallocators.h === --- /dev/null +++ test/support/instrumentingallocators.h @@ -0,0 +1,61 @@ +//===--===// +// +// The LLVM Compiler Infrastructure +// +// This file is dual licensed under the MIT and the University of Illinois Open +// Source Licenses. See LICENSE.TXT for details. +// +//===--===// + +#ifndef ALLOCATORS_H +#define ALLOCATORS_H + +#include +#include + +struct Instrumentation { +typedef std::unordered_map Allocations; + +Allocations allocs_; +size_t numAllocs_= 0; +size_t numDeallocs_ = 0; +size_t numAllocsLastChecked_ = 0; + +void checkAllocsIncreased() { +assert(numAllocs_ > numAllocsLastChecked_); +numAllocsLastChecked_ = numAllocs_; +} +}; + +template +struct IA1 { +Instrumentation *instrumentation_; + +typedef T value_type; + +explicit IA1(Instrumentation *instrumentation) : +instrumentation_(instrumentation) {} + +template +IA1(const IA1& other) : instrumentation_(other.instrumentation_) {} + +T *allocate(size_t n) { +void *result = std::malloc(sizeof(T) * n); +assert(instrumentation_->allocs_.find(result) == + instrumentation_->allocs_.end()); +instrumentation_->allocs_[result] = n; +++instrumentation_->numAllocs_; +return static_cast(result); +} + +void deallocate(T *ptr, size_t n) { +auto alloc = instrumentation_->allocs_.find(ptr); +assert(alloc != instrumentation_->allocs_.end()); +assert(alloc->second == n); +instrumentation_->allocs_.erase(alloc); +++instrumentation_->numDeallocs_; +std::free(ptr); +} +}; + +#endif Index: test/std/utilities/allocator.adaptor/scoped.adaptor.operators/construct.pass.cpp === --- /dev/null +++ test/std/utilities/allocator.adaptor/scoped.adaptor.operators/construct.pass.cpp @@ -0,0 +1,82 @@ +//===--===// +// +// The LLVM Compiler Infrastructure +// +// This file is dual licensed under the MIT and the University of Illinois Open +// Source Licenses. See LICENSE.TXT for details. +// +//===--===// + +// + +// template +// class scoped_allocator_adaptor + +// template void construct(T* p, Args&& args); +// template +// void construct(pair* p, piecewise_construct t, tuple x, +//tuple y); +// template +// void construct(pair* p); +// template +// void construct(pair* p, U&& x, V&& y); +// template +// void construct(pair* p, const pair& x); +// template +// void construct(pair* p, pair&& x); + +#include +#include +#include +#include + +#include "instrumentingallocators.h" + +namespace scoped { + +template +using IA1 = std::scoped_allocator_adaptor>; + +template +using List1 = std::list>; + +} + +int main() +{ +Instrumentation instrumentation; +{ +typedef scoped::List1 List; + +List::allocator_type alloc(&instrumentation); +List list(alloc); +list.emplace_back(); +instrumentation.checkAllocsIncreased(); +list.emplace_back(list.back()); +instrumentation.checkAllocsIncreased(); +} +assert(instrumentation.allocs_.size() == 0); +{ +typedef scoped::List1> List; + +List::allocator_type alloc(&instrumentation); +List list(alloc); +list.emplace_back(); +instrumentation.checkAllocsIncreased(); +list.emplace_back(list.back()); +instrumentation.checkAllocsIncreased(); +} +assert(instrumentation.allocs_.size() == 0); +{ +typedef scoped::List1> List
Re: [PATCH] D16970: scoped alloc construct: adds basic tests
frutiger updated this revision to Diff 47143. frutiger added a comment. Fixes include guards. http://reviews.llvm.org/D16970 Files: test/std/utilities/allocator.adaptor/scoped.adaptor.operators/construct.pass.cpp test/support/instrumentingallocators.h Index: test/support/instrumentingallocators.h === --- /dev/null +++ test/support/instrumentingallocators.h @@ -0,0 +1,75 @@ +//===--===// +// +// The LLVM Compiler Infrastructure +// +// This file is dual licensed under the MIT and the University of Illinois Open +// Source Licenses. See LICENSE.TXT for details. +// +//===--===// + +#ifndef INSTRUMENTINGALLOCATORS_H +#define INSTRUMENTINGALLOCATORS_H + +#include +#include + +struct Instrumentation { +typedef std::unordered_map Allocations; + +Allocations allocs_; +size_t numAllocs_= 0; +size_t numDeallocs_ = 0; +size_t numAllocsLastChecked_ = 0; + +~Instrumentation() { +assert(allocs_.empty()); +} + +void checkAllocsIncreased() { +assert(numAllocs_ > numAllocsLastChecked_); +numAllocsLastChecked_ = numAllocs_; +} +}; + +template +struct IA1 { +Instrumentation *instrumentation_; + +typedef T value_type; + +explicit IA1(Instrumentation *instrumentation) : +instrumentation_(instrumentation) {} + +template +IA1(const IA1& other) : instrumentation_(other.instrumentation_) {} + +T *allocate(size_t n) { +void *result = std::malloc(sizeof(T) * n); +assert(instrumentation_->allocs_.find(result) == + instrumentation_->allocs_.end()); +instrumentation_->allocs_[result] = n; +++instrumentation_->numAllocs_; +return static_cast(result); +} + +void deallocate(T *ptr, size_t n) { +auto alloc = instrumentation_->allocs_.find(ptr); +assert(alloc != instrumentation_->allocs_.end()); +assert(alloc->second == n); +instrumentation_->allocs_.erase(alloc); +++instrumentation_->numDeallocs_; +std::free(ptr); +} +}; + +template +bool operator==(const IA1& lhs, const IA1& rhs) { +return lhs.instrumentation_ == rhs.instrumentation_; +} + +template +bool operator!=(const IA1& lhs, const IA1& rhs) { +return !(lhs == rhs); +} + +#endif Index: test/std/utilities/allocator.adaptor/scoped.adaptor.operators/construct.pass.cpp === --- /dev/null +++ test/std/utilities/allocator.adaptor/scoped.adaptor.operators/construct.pass.cpp @@ -0,0 +1,82 @@ +//===--===// +// +// The LLVM Compiler Infrastructure +// +// This file is dual licensed under the MIT and the University of Illinois Open +// Source Licenses. See LICENSE.TXT for details. +// +//===--===// + +// + +// template +// class scoped_allocator_adaptor + +// template void construct(T* p, Args&& args); +// template +// void construct(pair* p, piecewise_construct t, tuple x, +//tuple y); +// template +// void construct(pair* p); +// template +// void construct(pair* p, U&& x, V&& y); +// template +// void construct(pair* p, const pair& x); +// template +// void construct(pair* p, pair&& x); + +#include +#include +#include +#include + +#include "instrumentingallocators.h" + +namespace scoped { + +template +using IA1 = std::scoped_allocator_adaptor>; + +template +using List1 = std::list>; + +} + +int main() +{ +Instrumentation instrumentation; +{ +typedef scoped::List1 List; + +List::allocator_type alloc(&instrumentation); +List list(alloc); +list.emplace_back(); +instrumentation.checkAllocsIncreased(); +list.emplace_back(list.back()); +instrumentation.checkAllocsIncreased(); +} +assert(instrumentation.allocs_.size() == 0); +{ +typedef scoped::List1> List; + +List::allocator_type alloc(&instrumentation); +List list(alloc); +list.emplace_back(); +instrumentation.checkAllocsIncreased(); +list.emplace_back(list.back()); +instrumentation.checkAllocsIncreased(); +} +assert(instrumentation.allocs_.size() == 0); +{ +typedef scoped::List1> List; + +List::allocator_type alloc(&instrumentation); +List list(alloc); +list.emplace_back(); +instrumentation.checkAllocsIncreased(); +list.emplace_back(list.back()); +instrumentation.checkAllocsIncreased(); +} +assert(instrumentation.allocs_.size() == 0); +} + ___ cfe-co