Parser.cpp 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041
  1. //===- Parser.cpp - Top-Level TableGen Parser implementation --------------===//
  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. #include "llvm/TableGen/Parser.h"
  9. #include "TGParser.h"
  10. #include "llvm/Support/MemoryBuffer.h"
  11. #include "llvm/TableGen/Record.h"
  12. using namespace llvm;
  13. bool llvm::TableGenParseFile(SourceMgr &InputSrcMgr, RecordKeeper &Records) {
  14. // Initialize the global TableGen source manager by temporarily taking control
  15. // of the input buffer in `SrcMgr`. This is kind of a hack, but allows for
  16. // preserving TableGen's current awkward diagnostic behavior. If we can remove
  17. // this reliance, we could drop all of this.
  18. SrcMgr = SourceMgr();
  19. SrcMgr.takeSourceBuffersFrom(InputSrcMgr);
  20. SrcMgr.setIncludeDirs(InputSrcMgr.getIncludeDirs());
  21. SrcMgr.setDiagHandler(InputSrcMgr.getDiagHandler(),
  22. InputSrcMgr.getDiagContext());
  23. // Setup the record keeper and try to parse the file.
  24. auto *MainFileBuffer = SrcMgr.getMemoryBuffer(SrcMgr.getMainFileID());
  25. Records.saveInputFilename(MainFileBuffer->getBufferIdentifier().str());
  26. TGParser Parser(SrcMgr, /*Macros=*/std::nullopt, Records,
  27. /*NoWarnOnUnusedTemplateArgs=*/false,
  28. /*TrackReferenceLocs=*/true);
  29. bool ParseResult = Parser.ParseFile();
  30. // After parsing, reclaim the source manager buffers from TableGen's global
  31. // manager.
  32. InputSrcMgr.takeSourceBuffersFrom(SrcMgr);
  33. SrcMgr = SourceMgr();
  34. return ParseResult;
  35. }