1f08c3bdfSopenharmony_ci/* 2f08c3bdfSopenharmony_ci * Example trivial client program that uses the sparse library 3f08c3bdfSopenharmony_ci * to tokenize, preprocess and parse a C file, and prints out 4f08c3bdfSopenharmony_ci * the results. 5f08c3bdfSopenharmony_ci * 6f08c3bdfSopenharmony_ci * Copyright (C) 2003 Transmeta Corp. 7f08c3bdfSopenharmony_ci * 2003 Linus Torvalds 8f08c3bdfSopenharmony_ci * 9f08c3bdfSopenharmony_ci * Permission is hereby granted, free of charge, to any person obtaining a copy 10f08c3bdfSopenharmony_ci * of this software and associated documentation files (the "Software"), to deal 11f08c3bdfSopenharmony_ci * in the Software without restriction, including without limitation the rights 12f08c3bdfSopenharmony_ci * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 13f08c3bdfSopenharmony_ci * copies of the Software, and to permit persons to whom the Software is 14f08c3bdfSopenharmony_ci * furnished to do so, subject to the following conditions: 15f08c3bdfSopenharmony_ci * 16f08c3bdfSopenharmony_ci * The above copyright notice and this permission notice shall be included in 17f08c3bdfSopenharmony_ci * all copies or substantial portions of the Software. 18f08c3bdfSopenharmony_ci * 19f08c3bdfSopenharmony_ci * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 20f08c3bdfSopenharmony_ci * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 21f08c3bdfSopenharmony_ci * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 22f08c3bdfSopenharmony_ci * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 23f08c3bdfSopenharmony_ci * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 24f08c3bdfSopenharmony_ci * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 25f08c3bdfSopenharmony_ci * THE SOFTWARE. 26f08c3bdfSopenharmony_ci */ 27f08c3bdfSopenharmony_ci#include <stdarg.h> 28f08c3bdfSopenharmony_ci#include <stdlib.h> 29f08c3bdfSopenharmony_ci#include <stdio.h> 30f08c3bdfSopenharmony_ci#include <string.h> 31f08c3bdfSopenharmony_ci#include <ctype.h> 32f08c3bdfSopenharmony_ci#include <unistd.h> 33f08c3bdfSopenharmony_ci#include <fcntl.h> 34f08c3bdfSopenharmony_ci 35f08c3bdfSopenharmony_ci#include "lib.h" 36f08c3bdfSopenharmony_ci#include "allocate.h" 37f08c3bdfSopenharmony_ci#include "token.h" 38f08c3bdfSopenharmony_ci#include "parse.h" 39f08c3bdfSopenharmony_ci#include "symbol.h" 40f08c3bdfSopenharmony_ci#include "expression.h" 41f08c3bdfSopenharmony_ci 42f08c3bdfSopenharmony_cistatic void clean_up_symbols(struct symbol_list *list) 43f08c3bdfSopenharmony_ci{ 44f08c3bdfSopenharmony_ci struct symbol *sym; 45f08c3bdfSopenharmony_ci 46f08c3bdfSopenharmony_ci FOR_EACH_PTR(list, sym) { 47f08c3bdfSopenharmony_ci expand_symbol(sym); 48f08c3bdfSopenharmony_ci } END_FOR_EACH_PTR(sym); 49f08c3bdfSopenharmony_ci} 50f08c3bdfSopenharmony_ci 51f08c3bdfSopenharmony_ciint main(int argc, char **argv) 52f08c3bdfSopenharmony_ci{ 53f08c3bdfSopenharmony_ci struct symbol_list * list; 54f08c3bdfSopenharmony_ci struct string_list * filelist = NULL; 55f08c3bdfSopenharmony_ci char *file; 56f08c3bdfSopenharmony_ci 57f08c3bdfSopenharmony_ci list = sparse_initialize(argc, argv, &filelist); 58f08c3bdfSopenharmony_ci 59f08c3bdfSopenharmony_ci // Simplification 60f08c3bdfSopenharmony_ci clean_up_symbols(list); 61f08c3bdfSopenharmony_ci 62f08c3bdfSopenharmony_ci#if 1 63f08c3bdfSopenharmony_ci show_symbol_list(list); 64f08c3bdfSopenharmony_ci printf("\n\n"); 65f08c3bdfSopenharmony_ci#endif 66f08c3bdfSopenharmony_ci 67f08c3bdfSopenharmony_ci FOR_EACH_PTR(filelist, file) { 68f08c3bdfSopenharmony_ci list = sparse(file); 69f08c3bdfSopenharmony_ci 70f08c3bdfSopenharmony_ci // Simplification 71f08c3bdfSopenharmony_ci clean_up_symbols(list); 72f08c3bdfSopenharmony_ci 73f08c3bdfSopenharmony_ci#if 1 74f08c3bdfSopenharmony_ci // Show the end result. 75f08c3bdfSopenharmony_ci show_symbol_list(list); 76f08c3bdfSopenharmony_ci printf("\n\n"); 77f08c3bdfSopenharmony_ci#endif 78f08c3bdfSopenharmony_ci } END_FOR_EACH_PTR(file); 79f08c3bdfSopenharmony_ci 80f08c3bdfSopenharmony_ci#if 0 81f08c3bdfSopenharmony_ci // And show the allocation statistics 82f08c3bdfSopenharmony_ci show_ident_alloc(); 83f08c3bdfSopenharmony_ci show_token_alloc(); 84f08c3bdfSopenharmony_ci show_symbol_alloc(); 85f08c3bdfSopenharmony_ci show_expression_alloc(); 86f08c3bdfSopenharmony_ci show_statement_alloc(); 87f08c3bdfSopenharmony_ci show_string_alloc(); 88f08c3bdfSopenharmony_ci show_bytes_alloc(); 89f08c3bdfSopenharmony_ci#endif 90f08c3bdfSopenharmony_ci return 0; 91f08c3bdfSopenharmony_ci} 92