Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Hello world llvm-lit with cfg file

Tags:

testing

llvm

I'm trying to run a simple test with llvm-lit. I have a dedicated directory:

llvm-lit-dir
+---lit.cfg
+---llvm_lit_example.c

The cfg file is from llvm-3.8.0/utils/lit/lit/ExampleTests.ObjDir. Here it is:

$ cat lit.cfg
config.example_obj_root = os.path.dirname(__file__)
lit.load_config(config, os.path.join(config.test_source_root,'lit.cfg'))

Here is the example I'm trying to check:

$ cat llvm_lit_example.c
// RUN: %clang -o %t0 %s
// RUN: %t0 | grep "YES"

#include <stdio.h>
#include <string.h>
#include <stdlib.h>

int main(int argc, char **argv)
{
    int i=rand()%4;

    char s1[5]={'0','1','2','3', 0 };
    char s2[5]={'0','1','2','3', 0 };

    s1[i]='6';

    if (strcmp(s1,s2) == 0) printf("NO \n");
    if (strcmp(s1,s2) != 0) printf("YES\n");
}

I've tried several options but none seem to work:

$ llvm-lit llvm-lit-dir/llvm_lit_example.c
llvm-lit: TestingConfig.py:114: fatal: unable to parse config file '/home//llvm-lit-dir/lit.site.cfg', traceback: Traceback (most recent call last):
File "/home/llvm-3.8.0/llvm/utils/lit/lit/TestingConfig.py", line 101, in load_from_path
  exec(compile(data, path, 'exec'), cfg_globals, None)
File "/home/llvm-lit-dir/lit.site.cfg", line 14, in <module>
  lit.load_config(config, os.path.join(config.test_source_root,'lit.cfg'))
NameError: name 'lit' is not defined

What am I doing wrong? Thanks!

like image 278
OrenIshShalom Avatar asked Oct 17 '22 16:10

OrenIshShalom


1 Answers

The following is the minimalistic "hello world" example which is now working for me:

config file: lit.cfg

import lit.formats

config.name = "LIT hello world"
config.test_format = lit.formats.ShTest("0")

test file: tests/test

; RUN: echo "Foo" | FileCheck %s
; CHECK: Foo

run it with

lit -v tests/test

To make the test fail, change it to echo "Bar".


Two additional comments:

1) Installing lit is easy:

pip3 install lit

To install FileCheck I had to download LLVM source code (LLVM Download page) and build the FileCheck target using CMake.

2) config.test_format = lit.formats.ShTest("0") is important because otherwise lit fails with the following error:

AttributeError: 'NoneType' object has no attribute 'execute'

It looks like ShTest is a default format, but a user still has to activate it manually.

like image 129
Stanislav Pankevich Avatar answered Oct 21 '22 04:10

Stanislav Pankevich