diff options
Diffstat (limited to 'clang-tools-extra/clang-doc')
| -rw-r--r-- | clang-tools-extra/clang-doc/gen_tests.py | 227 | ||||
| -rw-r--r-- | clang-tools-extra/clang-doc/tool/ClangDocMain.cpp | 78 |
2 files changed, 14 insertions, 291 deletions
diff --git a/clang-tools-extra/clang-doc/gen_tests.py b/clang-tools-extra/clang-doc/gen_tests.py deleted file mode 100644 index 9259684d588..00000000000 --- a/clang-tools-extra/clang-doc/gen_tests.py +++ /dev/null @@ -1,227 +0,0 @@ -#!/usr/bin/env python3 -# -#===- gen_tests.py - clang-doc test generator ----------------*- python -*--===# -# -# The LLVM Compiler Infrastructure -# -# This file is distributed under the University of Illinois Open Source -# License. See LICENSE.TXT for details. -# -#===------------------------------------------------------------------------===# -""" -clang-doc test generator -========================== - -Generates tests for clang-doc given a certain set of flags, a prefix for the -test file, and a given clang-doc binary. Please check emitted tests for -accuracy before using. - -To generate all current tests: -- Generate mapper tests: - python gen_tests.py -flag='--dump-mapper' -flag='--doxygen' -flag='--extra-arg=-fmodules-ts' -prefix mapper -use-check-next - -- Generate reducer tests: - python gen_tests.py -flag='--dump-intermediate' -flag='--doxygen' -flag='--extra-arg=-fmodules-ts' -prefix bc -use-check-next - -- Generate yaml tests: - python gen_tests.py -flag='--format=yaml' -flag='--doxygen' -flag='--extra-arg=-fmodules-ts' -prefix yaml -use-check-next - -- Generate public decl tests: - python gen_tests.py -flag='--format=yaml' -flag='--doxygen' -flag='--public' -flag='--extra-arg=-fmodules-ts' -prefix public -use-check-next - -- Generate Markdown tests: - python gen_tests.py -flag='--format=md' -flag='--doxygen' -flag='--public' -flag='--extra-arg=-fmodules-ts' -prefix md - -This script was written on/for Linux, and has not been tested on any other -platform and so it may not work. - -""" - -import argparse -import glob -import os -import re -import shutil -import subprocess - -RUN_CLANG_DOC = """ -// RUN: clang-doc {0} -p %t %t/test.cpp -output=%t/docs -""" -RUN = """ -// RUN: {0} %t/{1} | FileCheck %s --check-prefix CHECK-{2} -""" - -CHECK = '// CHECK-{0}: ' - -CHECK_NEXT = '// CHECK-{0}-NEXT: ' - -BITCODE_USR = '<USR abbrevid=4 op0=20 op1={{[0-9]+}} op2={{[0-9]+}} op3={{[0-9]+}} op4={{[0-9]+}} op5={{[0-9]+}} op6={{[0-9]+}} op7={{[0-9]+}} op8={{[0-9]+}} op9={{[0-9]+}} op10={{[0-9]+}} op11={{[0-9]+}} op12={{[0-9]+}} op13={{[0-9]+}} op14={{[0-9]+}} op15={{[0-9]+}} op16={{[0-9]+}} op17={{[0-9]+}} op18={{[0-9]+}} op19={{[0-9]+}} op20={{[0-9]+}}/>' -BITCODE_USR_REGEX = r'<USR abbrevid=4 op0=20 op1=[0-9]+ op2=[0-9]+ op3=[0-9]+ op4=[0-9]+ op5=[0-9]+ op6=[0-9]+ op7=[0-9]+ op8=[0-9]+ op9=[0-9]+ op10=[0-9]+ op11=[0-9]+ op12=[0-9]+ op13=[0-9]+ op14=[0-9]+ op15=[0-9]+ op16=[0-9]+ op17=[0-9]+ op18=[0-9]+ op19=[0-9]+ op20=[0-9]+/>' -YAML_USR = "USR: '{{[0-9A-Z][0-9A-Z][0-9A-Z][0-9A-Z][0-9A-Z][0-9A-Z][0-9A-Z][0-9A-Z][0-9A-Z][0-9A-Z][0-9A-Z][0-9A-Z][0-9A-Z][0-9A-Z][0-9A-Z][0-9A-Z][0-9A-Z][0-9A-Z][0-9A-Z][0-9A-Z][0-9A-Z][0-9A-Z][0-9A-Z][0-9A-Z][0-9A-Z][0-9A-Z][0-9A-Z][0-9A-Z][0-9A-Z][0-9A-Z][0-9A-Z][0-9A-Z][0-9A-Z][0-9A-Z][0-9A-Z][0-9A-Z][0-9A-Z][0-9A-Z][0-9A-Z][0-9A-Z]}}'" -YAML_USR_REGEX = r"USR: '[0-9A-Z][0-9A-Z][0-9A-Z][0-9A-Z][0-9A-Z][0-9A-Z][0-9A-Z][0-9A-Z][0-9A-Z][0-9A-Z][0-9A-Z][0-9A-Z][0-9A-Z][0-9A-Z][0-9A-Z][0-9A-Z][0-9A-Z][0-9A-Z][0-9A-Z][0-9A-Z][0-9A-Z][0-9A-Z][0-9A-Z][0-9A-Z][0-9A-Z][0-9A-Z][0-9A-Z][0-9A-Z][0-9A-Z][0-9A-Z][0-9A-Z][0-9A-Z][0-9A-Z][0-9A-Z][0-9A-Z][0-9A-Z][0-9A-Z][0-9A-Z][0-9A-Z][0-9A-Z]'" - -def clear_test_prefix_files(prefix, tests_path): - if os.path.isdir(tests_path): - for root, dirs, files in os.walk(tests_path): - for filename in files: - if filename.startswith(prefix): - os.remove(os.path.join(root, filename)) - - -def copy_to_test_file(test_case_path, test_cases_path): - # Copy file to 'test.cpp' to preserve file-dependent USRs - test_file = os.path.join(test_cases_path, 'test.cpp') - shutil.copyfile(test_case_path, test_file) - return test_file - - -def run_clang_doc(args, out_dir, test_file): - # Run clang-doc. - current_cmd = [args.clangdoc] - current_cmd.extend(args.flags) - current_cmd.append('--output=' + out_dir) - current_cmd.append(test_file) - print('Running ' + ' '.join(current_cmd)) - return_code = subprocess.call(current_cmd) - if return_code: - return 1 - return 0 - - -def get_test_case_code(test_case_path, flags): - # Get the test case code - code = '' - with open(test_case_path, 'r') as code_file: - code = code_file.read() - - code += RUN_CLANG_DOC.format(flags) - return code - - -def get_output(root, out_file, case_out_path, flags, checkname, bcanalyzer, - check_next=True): - output = '' - run_cmd = '' - if '--dump-mapper' in flags or '--dump-intermediate' in flags: - # Run llvm-bcanalyzer - output = subprocess.check_output( - [bcanalyzer, '--dump', - os.path.join(root, out_file)]) - output = output[:output.find('Summary of ')].rstrip() - run_cmd = RUN.format('llvm-bcanalyzer --dump', - os.path.join('docs', 'bc', out_file), checkname) - else: - # Run cat - output = subprocess.check_output(['cat', os.path.join(root, out_file)]) - run_cmd = RUN.format( - 'cat', - os.path.join('docs', os.path.relpath(root, case_out_path), - out_file), checkname) - - # Format output. - output = output.replace('blob data = \'test\'', 'blob data = \'{{.*}}\'') - output = re.sub(YAML_USR_REGEX, YAML_USR, output) - output = re.sub(BITCODE_USR_REGEX, BITCODE_USR, output) - output = CHECK.format(checkname) + output.rstrip() - - if check_next: - check_comment = CHECK_NEXT.format(checkname) - else: - check_comment = CHECK.format(checkname) - - output = output.replace('\n', '\n' + check_comment) - output = run_cmd + output.replace('%s\n' % check_comment, "") - - return output + '\n' - - -def main(): - parser = argparse.ArgumentParser(description='Generate clang-doc tests.') - parser.add_argument( - '-flag', - action='append', - default=[], - dest='flags', - help='Flags to pass to clang-doc.') - parser.add_argument( - '-prefix', - type=str, - default='', - dest='prefix', - help='Prefix for this test group.') - parser.add_argument( - '-clang-doc-binary', - dest='clangdoc', - metavar="PATH", - default='clang-doc', - help='path to clang-doc binary') - parser.add_argument( - '-llvm-bcanalyzer-binary', - dest='bcanalyzer', - metavar="PATH", - default='llvm-bcanalyzer', - help='path to llvm-bcanalyzer binary') - parser.add_argument( - '-use-check-next', - dest='check_next', - default=False, - action='store_true', - help='Whether or not to use CHECK-NEXT in the resulting tests.') - args = parser.parse_args() - - flags = ' '.join(args.flags) - - clang_doc_path = os.path.dirname(__file__) - tests_path = os.path.join(clang_doc_path, '..', 'test', 'clang-doc') - test_cases_path = os.path.join(tests_path, 'test_cases') - - clear_test_prefix_files(args.prefix, tests_path) - - for test_case_path in glob.glob(os.path.join(test_cases_path, '*')): - if test_case_path.endswith( - 'compile_flags.txt') or test_case_path.endswith( - 'compile_commands.json'): - continue - - # Name of this test case - case_name = os.path.basename(test_case_path).split('.')[0] - - test_file = copy_to_test_file(test_case_path, test_cases_path) - out_dir = os.path.join(test_cases_path, case_name) - - if run_clang_doc(args, out_dir, test_file): - return 1 - - # Retrieve output and format as FileCheck tests - all_output = '' - num_outputs = 0 - for root, dirs, files in os.walk(out_dir): - for out_file in files: - # Make the file check the first 3 letters (there's a very small chance - # that this will collide, but the fix is to simply change the decl name) - usr = os.path.basename(out_file).split('.') - # If the usr is less than 2, this isn't one of the test files. - if len(usr) < 2: - continue - all_output += get_output(root, out_file, out_dir, args.flags, - num_outputs, args.bcanalyzer, - args.check_next) - num_outputs += 1 - - # Add test case code to test - all_output = get_test_case_code(test_case_path, - flags) + '\n' + all_output - - # Write to test case file in /test. - test_out_path = os.path.join( - tests_path, args.prefix + '-' + os.path.basename(test_case_path)) - with open(test_out_path, 'w+') as o: - o.write(all_output) - - # Clean up - shutil.rmtree(out_dir) - os.remove(test_file) - - -if __name__ == '__main__': - main() diff --git a/clang-tools-extra/clang-doc/tool/ClangDocMain.cpp b/clang-tools-extra/clang-doc/tool/ClangDocMain.cpp index 6b50f6ca838..d8559aae2b5 100644 --- a/clang-tools-extra/clang-doc/tool/ClangDocMain.cpp +++ b/clang-tools-extra/clang-doc/tool/ClangDocMain.cpp @@ -56,19 +56,14 @@ static llvm::cl::opt<std::string> llvm::cl::init("docs"), llvm::cl::cat(ClangDocCategory)); static llvm::cl::opt<bool> - DumpMapperResult("dump-mapper", - llvm::cl::desc("Dump mapper results to bitcode file."), - llvm::cl::init(false), llvm::cl::cat(ClangDocCategory)); - -static llvm::cl::opt<bool> DumpIntermediateResult( - "dump-intermediate", - llvm::cl::desc("Dump intermediate results to bitcode file."), - llvm::cl::init(false), llvm::cl::cat(ClangDocCategory)); - -static llvm::cl::opt<bool> PublicOnly("public", llvm::cl::desc("Document only public declarations."), llvm::cl::init(false), llvm::cl::cat(ClangDocCategory)); +static llvm::cl::opt<bool> DoxygenOnly( + "doxygen", + llvm::cl::desc("Use only doxygen-style comments to generate docs."), + llvm::cl::init(false), llvm::cl::cat(ClangDocCategory)); + enum OutputFormatTy { md, yaml, @@ -83,10 +78,15 @@ static llvm::cl::opt<OutputFormatTy> llvm::cl::init(OutputFormatTy::yaml), llvm::cl::cat(ClangDocCategory)); -static llvm::cl::opt<bool> DoxygenOnly( - "doxygen", - llvm::cl::desc("Use only doxygen-style comments to generate docs."), - llvm::cl::init(false), llvm::cl::cat(ClangDocCategory)); +std::string getFormatString() { + switch (FormatEnum) { + case OutputFormatTy::yaml: + return "yaml"; + case OutputFormatTy::md: + return "md"; + } + llvm_unreachable("Unknown OutputFormatTy"); +} bool CreateDirectory(const Twine &DirName, bool ClearDirectory = false) { std::error_code OK; @@ -107,26 +107,6 @@ bool CreateDirectory(const Twine &DirName, bool ClearDirectory = false) { return false; } -bool DumpResultToFile(const Twine &DirName, const Twine &FileName, - StringRef Buffer, bool ClearDirectory = false) { - std::error_code OK; - llvm::SmallString<128> IRRootPath; - llvm::sys::path::native(OutDirectory, IRRootPath); - llvm::sys::path::append(IRRootPath, DirName); - if (CreateDirectory(IRRootPath, ClearDirectory)) - return true; - llvm::sys::path::append(IRRootPath, FileName); - std::error_code OutErrorInfo; - llvm::raw_fd_ostream OS(IRRootPath, OutErrorInfo, llvm::sys::fs::F_None); - if (OutErrorInfo != OK) { - llvm::errs() << "Error opening documentation file.\n"; - return true; - } - OS << Buffer; - OS.close(); - return false; -} - // A function to extract the appropriate path name for a given info's // documentation. The path returned is a composite of the parent namespaces as // directories plus the decl name as the filename. @@ -161,16 +141,6 @@ getInfoOutputFile(StringRef Root, return Path; } -std::string getFormatString() { - switch (FormatEnum) { - case OutputFormatTy::yaml: - return "yaml"; - case OutputFormatTy::md: - return "md"; - } - llvm_unreachable("Unknown OutputFormatTy"); -} - // Iterate through tool results and build string map of info vectors from the // encoded bitstreams. bool bitcodeResultsToInfos( @@ -234,17 +204,6 @@ int main(int argc, const char **argv) { return 1; } - if (DumpMapperResult) { - bool Err = false; - Exec->get()->getToolResults()->forEachResult( - [&](StringRef Key, StringRef Value) { - Err = DumpResultToFile("bc", Key + ".bc", Value); - }); - if (Err) - llvm::errs() << "Error dumping map results.\n"; - return Err; - } - // Collect values into output by key. // In ToolResults, the Key is the hashed USR and the value is the // bitcode-encoded representation of the Info object. @@ -262,15 +221,6 @@ int main(int argc, const char **argv) { continue; } - if (DumpIntermediateResult) { - SmallString<4096> Buffer; - llvm::BitstreamWriter Stream(Buffer); - doc::ClangDocBitcodeWriter Writer(Stream); - Writer.dispatchInfoForWrite(Reduced.get().get()); - if (DumpResultToFile("bc", Group.getKey() + ".bc", Buffer)) - llvm::errs() << "Error dumping to bitcode.\n"; - continue; - } doc::Info *I = Reduced.get().get(); auto InfoPath = |

