analysis.go raw

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