This is an archive of the discontinued LLVM Phabricator instance.

[libc] Add scaffolding for ctype and implementation of isalpha
ClosedPublic

Authored by cgyurgyik on Jul 24 2020, 6:32 PM.

Diff Detail

Event Timeline

cgyurgyik created this revision.Jul 24 2020, 6:32 PM
abrachet added inline comments.
libc/config/linux/aarch64/entrypoints.txt
5

Nit: Maybe these should go above errno.h entrypoints for alphabetical order

libc/config/linux/api.td
408

Presumably not necessary? You don't have empty Macros so presumably it is fine to leave this out for now.

libc/src/ctype/isalpha.cpp
16

Just use unsigned

libc/test/src/ctype/isalpha_test.cpp
14

You don't need the cast.

26–34

I don't understand the comment, there is no implementation where character would pass but not character++. You're testing the compiler here, not isalpha

cgyurgyik added inline comments.Jul 25 2020, 3:54 AM
libc/test/src/ctype/isalpha_test.cpp
26–34

This was written after reading the wikipedia page: https://en.wikipedia.org/wiki/C_character_classification#Implementation

I am happy to remove it if you think this is a compiler test instead.

cgyurgyik updated this revision to Diff 280664.Jul 25 2020, 4:01 AM
cgyurgyik marked 4 inline comments as done.

Removed CType from libc/config/linux/api.td, as well as other comment responses.

cgyurgyik updated this revision to Diff 280666.Jul 25 2020, 4:17 AM
sivachandra added inline comments.Jul 27 2020, 10:57 PM
libc/include/ctype.h.def
2

I think we have a ctype.h file already present. It was added when I was setting up examples. But, ctype.h.def as you have added is the right approach. Can you remove ctype.h in a separate patch and add appropriate targets for ctype.h.def in this patch? Also, you do need to add an entry to linux/api.td isn't it?

libc/src/ctype/isalpha.h
15

Can you move this TODO to the .cpp file?

libc/test/src/ctype/isalpha_test.cpp
12

Wouldn't it be simple if we have just one test like this:

TEST(IsAlpha, DefaultLocale) {
  for (int i = 0; i < 255; ++i) {
    if (('a' <= i && i <= 'z') || ('A' <= i && i <= 'Z')
      ASSERT_TRUE(...);
    else
      ASSERT_FALSE(...);
  }
}
cgyurgyik marked an inline comment as done.

Update unit tests to a single test that goes through all characters 0-255.
Re-add linux/api.td.
Address other comments

cgyurgyik marked 2 inline comments as done.Jul 28 2020, 10:13 AM
cgyurgyik added inline comments.
libc/include/ctype.h.def
2

I will remove ctype.h immediately following the submission of this revision.

libc/test/src/ctype/isalpha_test.cpp
12

Wouldn't it be simple if we have just one test like this:

TEST(IsAlpha, DefaultLocale) {
  for (int i = 0; i < 255; ++i) {
    if (('a' <= i && i <= 'z') || ('A' <= i && i <= 'Z')
      ASSERT_TRUE(...);
    else
      ASSERT_FALSE(...);
  }
}

Yep that works as well. It definitely breaks the idea of unit testing different behaviors, but isalpha is probably simple enough that this won't matter a whole lot. I mostly wasn't sure if 255 calls to is* would get hefty after a few more of these are implemented.

cgyurgyik marked 2 inline comments as done.
sivachandra accepted this revision.Jul 28 2020, 10:36 AM
This revision is now accepted and ready to land.Jul 28 2020, 10:36 AM