analysis.go raw

   1  //===- analysis.go - Bindings for analysis --------------------------------===//
   2  //
   3  // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
   4  // See https://llvm.org/LICENSE.txt for license information.
   5  // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
   6  //
   7  //===----------------------------------------------------------------------===//
   8  //
   9  // This file defines bindings for the analysis component.
  10  //
  11  //===----------------------------------------------------------------------===//
  12  
  13  package llvm
  14  
  15  /*
  16  #include "llvm-c/Analysis.h" // If you are getting an error here you need to build or install LLVM, see https://tinygo.org/docs/guides/build/
  17  #include "llvm-c/Core.h"
  18  #include <stdlib.h>
  19  */
  20  import "C"
  21  import "errors"
  22  
  23  type VerifierFailureAction C.LLVMVerifierFailureAction
  24  
  25  const (
  26  	// verifier will print to stderr and abort()
  27  	AbortProcessAction VerifierFailureAction = C.LLVMAbortProcessAction
  28  	// verifier will print to stderr and return 1
  29  	PrintMessageAction VerifierFailureAction = C.LLVMPrintMessageAction
  30  	// verifier will just return 1
  31  	ReturnStatusAction VerifierFailureAction = C.LLVMReturnStatusAction
  32  )
  33  
  34  // Verifies that a module is valid, taking the specified action if not.
  35  // Optionally returns a human-readable description of any invalid constructs.
  36  func VerifyModule(m Module, a VerifierFailureAction) error {
  37  	var cmsg *C.char
  38  	broken := C.LLVMVerifyModule(m.C, C.LLVMVerifierFailureAction(a), &cmsg)
  39  
  40  	// C++'s verifyModule means isModuleBroken, so it returns false if
  41  	// there are no errors
  42  	if broken != 0 {
  43  		err := errors.New(C.GoString(cmsg))
  44  		C.LLVMDisposeMessage(cmsg)
  45  		return err
  46  	}
  47  	return nil
  48  }
  49  
  50  var verifyFunctionError = errors.New("Function is broken")
  51  
  52  // Verifies that a single function is valid, taking the specified action.
  53  // Useful for debugging.
  54  func VerifyFunction(f Value, a VerifierFailureAction) error {
  55  	broken := C.LLVMVerifyFunction(f.C, C.LLVMVerifierFailureAction(a))
  56  
  57  	// C++'s verifyFunction means isFunctionBroken, so it returns false if
  58  	// there are no errors
  59  	if broken != 0 {
  60  		return verifyFunctionError
  61  	}
  62  	return nil
  63  }
  64  
  65  // Open up a ghostview window that displays the CFG of the current function.
  66  // Useful for debugging.
  67  func ViewFunctionCFG(f Value)     { C.LLVMViewFunctionCFG(f.C) }
  68  func ViewFunctionCFGOnly(f Value) { C.LLVMViewFunctionCFGOnly(f.C) }
  69