Symbolizer refactoring: ExtractToken and friends

Reviewed at http://reviews.llvm.org/D7867

llvm-svn: 231027
This commit is contained in:
Kuba Brecka 2015-03-02 22:15:14 +00:00
parent e215594013
commit 59e8c3b342
5 changed files with 89 additions and 39 deletions

View File

@ -18,6 +18,14 @@
namespace __sanitizer {
// Parsing helpers, 'str' is searched for delimiter(s) and a string or uptr
// is extracted. When extracting a string, a newly allocated (using
// InternalAlloc) and null-terminataed buffer is returned. They return a pointer
// to the next characted after the found delimiter.
const char *ExtractToken(const char *str, const char *delims, char **result);
const char *ExtractInt(const char *str, const char *delims, int *result);
const char *ExtractUptr(const char *str, const char *delims, uptr *result);
class SymbolizerTool {
public:
// Can't declare pure virtual functions in sanitizer runtimes:

View File

@ -11,11 +11,42 @@
// run-time libraries.
//===----------------------------------------------------------------------===//
#include "sanitizer_allocator_internal.h"
#include "sanitizer_internal_defs.h"
#include "sanitizer_symbolizer_internal.h"
namespace __sanitizer {
const char *ExtractToken(const char *str, const char *delims, char **result) {
uptr prefix_len = internal_strcspn(str, delims);
*result = (char*)InternalAlloc(prefix_len + 1);
internal_memcpy(*result, str, prefix_len);
(*result)[prefix_len] = '\0';
const char *prefix_end = str + prefix_len;
if (*prefix_end != '\0') prefix_end++;
return prefix_end;
}
const char *ExtractInt(const char *str, const char *delims, int *result) {
char *buff;
const char *ret = ExtractToken(str, delims, &buff);
if (buff != 0) {
*result = (int)internal_atoll(buff);
}
InternalFree(buff);
return ret;
}
const char *ExtractUptr(const char *str, const char *delims, uptr *result) {
char *buff;
const char *ret = ExtractToken(str, delims, &buff);
if (buff != 0) {
*result = (uptr)internal_atoll(buff);
}
InternalFree(buff);
return ret;
}
Symbolizer *Symbolizer::GetOrInit() {
SpinMutexLock l(&init_mu_);
if (symbolizer_)

View File

@ -52,45 +52,6 @@ static const char *DemangleCXXABI(const char *name) {
return name;
}
// Extracts the prefix of "str" that consists of any characters not
// present in "delims" string, and copies this prefix to "result", allocating
// space for it.
// Returns a pointer to "str" after skipping extracted prefix and first
// delimiter char.
static const char *ExtractToken(const char *str, const char *delims,
char **result) {
uptr prefix_len = internal_strcspn(str, delims);
*result = (char*)InternalAlloc(prefix_len + 1);
internal_memcpy(*result, str, prefix_len);
(*result)[prefix_len] = '\0';
const char *prefix_end = str + prefix_len;
if (*prefix_end != '\0') prefix_end++;
return prefix_end;
}
// Same as ExtractToken, but converts extracted token to integer.
static const char *ExtractInt(const char *str, const char *delims,
int *result) {
char *buff;
const char *ret = ExtractToken(str, delims, &buff);
if (buff != 0) {
*result = (int)internal_atoll(buff);
}
InternalFree(buff);
return ret;
}
static const char *ExtractUptr(const char *str, const char *delims,
uptr *result) {
char *buff;
const char *ret = ExtractToken(str, delims, &buff);
if (buff != 0) {
*result = (uptr)internal_atoll(buff);
}
InternalFree(buff);
return ret;
}
// Parses one or more two-line strings in the following format:
// <function_name>
// <file_name>:<line_number>[:<column_number>]

View File

@ -28,6 +28,7 @@ set(SANITIZER_UNITTESTS
sanitizer_stacktrace_test.cc
sanitizer_stoptheworld_test.cc
sanitizer_suppressions_test.cc
sanitizer_symbolizer_test.cc
sanitizer_test_main.cc
sanitizer_thread_registry_test.cc)

View File

@ -0,0 +1,49 @@
//===-- sanitizer_symbolizer_test.cc --------------------------------------===//
//
// The LLVM Compiler Infrastructure
//
// This file is distributed under the University of Illinois Open Source
// License. See LICENSE.TXT for details.
//
//===----------------------------------------------------------------------===//
//
// Tests for sanitizer_symbolizer.h and sanitizer_symbolizer_internal.h
//
//===----------------------------------------------------------------------===//
#include "sanitizer_common/sanitizer_allocator_internal.h"
#include "sanitizer_common/sanitizer_symbolizer_internal.h"
#include "gtest/gtest.h"
namespace __sanitizer {
TEST(Symbolizer, ExtractToken) {
char *token;
const char *rest;
rest = ExtractToken("a;b;c", ";", &token);
EXPECT_STREQ("a", token);
EXPECT_STREQ("b;c", rest);
InternalFree(token);
rest = ExtractToken("aaa-bbb.ccc", ";.-*", &token);
EXPECT_STREQ("aaa", token);
EXPECT_STREQ("bbb.ccc", rest);
InternalFree(token);
}
TEST(Symbolizer, ExtractInt) {
int token;
const char *rest = ExtractInt("123,456;789", ";,", &token);
EXPECT_EQ(123, token);
EXPECT_STREQ("456;789", rest);
}
TEST(Symbolizer, ExtractUptr) {
uptr token;
const char *rest = ExtractUptr("123,456;789", ";,", &token);
EXPECT_EQ(123U, token);
EXPECT_STREQ("456;789", rest);
}
} // namespace __sanitizer