Skip to content

Commit

Permalink
Added explicit test for literals.
Browse files Browse the repository at this point in the history
  • Loading branch information
freakboy3742 committed Feb 4, 2016
1 parent c128bb6 commit f12a25e
Show file tree
Hide file tree
Showing 5 changed files with 48 additions and 8 deletions.
10 changes: 10 additions & 0 deletions README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -169,6 +169,16 @@ aren't exposed as symbols for third-party use.
All this functionality is potentially a candidate to be passed upstream to
libclang.

Relationship to Clang
~~~~~~~~~~~~~~~~~~~~~

This project aims to mirror what is currently available in the Python bindings
to ``libclang``. The version number for this project is drawn from the version
and SVN revision of the official clang repository.

Any changes made upstream to ``libclang`` will be mirrored here; any changes
made here will, where possible, be pushed upstream to ``libclang``.

Community
---------

Expand Down
2 changes: 1 addition & 1 deletion clang/cindex.py
Original file line number Diff line number Diff line change
Expand Up @@ -1324,7 +1324,7 @@ def literal(self):
Retrieves the literal at this cursor
"""
if not hasattr(self, '_literal'):
self._literal = conf.sealang.clang_Cursor_getLiteralString(self).decode('utf-8')
self._literal = conf.sealang.clang_Cursor_getLiteralString(self)

return self._literal

Expand Down
9 changes: 3 additions & 6 deletions sealang/sealang.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
#include "clang/AST/Stmt.h"
#include "clang/AST/Expr.h"
#include "clang/AST/ExprCXX.h"
#include "llvm/ADT/SmallString.h"

/************************************************************************
* Duplicated libclang functionality
Expand Down Expand Up @@ -121,13 +122,9 @@ CXString clang_Cursor_getLiteralString(CXCursor cursor)

if (cursor.kind == CXCursor_FloatingLiteral) {
clang::FloatingLiteral *floatLiteral = (clang::FloatingLiteral *) clang::getCursorExpr(cursor);
// This is the code needed in clang 3.9
// llvm::SmallString<1024> str;
// floatLiteral->getValue().toString(str);
// return clang::cxstring::createDup(str.c_str());
llvm::SmallVector<char, 1024> str;
llvm::SmallString<1024> str;
floatLiteral->getValue().toString(str);
return clang::cxstring::createDup(str.data());
return clang::cxstring::createDup(str.c_str());
}

if (cursor.kind == CXCursor_CharacterLiteral) {
Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@

setup(
name='sealang',
version='3.9.dev258341',
version='3.9.dev259721',
description='An extended set of Python bindings for libclang',
long_description=open('README.rst').read(),
url='https://github.com/pybee/sealang',
Expand Down
33 changes: 33 additions & 0 deletions tests/cindex/test_cursor.py
Original file line number Diff line number Diff line change
Expand Up @@ -448,6 +448,39 @@ def test_binary_operator():
assert False, "Operator %s (%s) not found in test data" % (operator, spelling)


def test_literals():
tu = get_tu("""
struct C {
int m;
};
void func(void) {
int intLiteral = 37;
float floatLiteral = 99.5;
char charLiteral = 'q';
char *stringLiteral = "Hello, World";
bool boolLiteral = true;
}
""", lang="cpp")

literals = {
CursorKind.INTEGER_LITERAL: b'37',
CursorKind.FLOATING_LITERAL: b'99.5',
CursorKind.CHARACTER_LITERAL: b'q',
CursorKind.STRING_LITERAL: b'Hello, World',
CursorKind.CXX_BOOL_LITERAL_EXPR: b'true'
}

for literal, spelling in literals.items():
found = False
for cursor in tu.cursor.walk_preorder():
if cursor.kind == literal:
assert cursor.literal == spelling, "Problem with %s; %s != %s" % (literal, cursor.literal, spelling)
found = True
if not found:
assert False, "%s '%s' not found in test data" % (literal, spelling)


def test_annotation_attribute():
tu = get_tu('int foo (void) __attribute__ ((annotate("here be annotation attribute")));')

Expand Down

0 comments on commit f12a25e

Please sign in to comment.