<commit_before>//===-- Frontend.cpp - frontend utility methods ---------------------------===//
//
// This source file is part of the Swift.org open source project
//
// Copyright (c) 2014 - 2015 Apple Inc. and the Swift project authors
// Licensed under Apache License v2.0 with Runtime Library Exception
//
// See http://swift.org/LICENSE.txt for license information
// See http://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
//
//===----------------------------------------------------------------------===//
//
// This file contains utility methods for parsing and performing semantic
// on modules.
//
//===----------------------------------------------------------------------===//

#include "Frontend.h"
#include "swift/AST/Identifier.h"
#include "swift/AST/ASTContext.h"
#include "swift/AST/Component.h"
#include "swift/AST/Diagnostics.h"
#include "swift/AST/Module.h"
#include "swift/AST/Stmt.h"
#include "swift/Parse/Lexer.h"
#include "swift/Subsystems.h"
#include "llvm/Support/MemoryBuffer.h"
#include "llvm/Support/PathV2.h"
#include "llvm/Support/SourceMgr.h"

using namespace swift;

static Identifier getModuleIdentifier(const llvm::MemoryBuffer *Buffer, ASTContext &Context) {
  StringRef moduleName = Buffer->getBufferIdentifier();

  // As a special case, recognize <stdin>.
  if (moduleName == "<stdin>")
    return Context.getIdentifier("stdin");

  // Find the stem of the filename.
  moduleName = llvm::sys::path::stem(moduleName);

  // Complain about non-identifier characters in the module name.
  if (!Lexer::isIdentifier(moduleName)) {
    SourceLoc Loc = SourceLoc(llvm::SMLoc::getFromPointer(Buffer->getBuffer().begin()));
    Context.Diags.diagnose(Loc, diag::bad_module_name);
    moduleName = "bad";
  }

  return Context.getIdentifier(moduleName);
}

TranslationUnit*
swift::buildSingleTranslationUnit(ASTContext &Context, unsigned BufferID,
                                  bool ParseOnly, bool IsMainModule) {
  Component *Comp = new (Context.Allocate<Component>(1)) Component();
  const llvm::MemoryBuffer *Buffer = Context.SourceMgr.getMemoryBuffer(BufferID);
  Identifier ID = getModuleIdentifier(Buffer, Context);
  TranslationUnit *TU = new (Context) TranslationUnit(ID, Comp, Context,
                                                      IsMainModule,
                                                      /*IsReplModule=*/false);

  unsigned BufferOffset = 0;
  unsigned CurTUElem = 0;
  do {
    parseIntoTranslationUnit(TU, BufferID, &BufferOffset);
    if (!ParseOnly) {
      performNameBinding(TU, CurTUElem);
      performTypeChecking(TU, CurTUElem);
      CurTUElem = TU->Decls.size();
    }
  } while (BufferOffset != Buffer->getBufferSize());

  return TU;<commit_msg>When parsing in script mode, the file name may not be a valid identifier.<commit_after>//===-- Frontend.cpp - frontend utility methods ---------------------------===//
//
// This source file is part of the Swift.org open source project
//
// Copyright (c) 2014 - 2015 Apple Inc. and the Swift project authors
// Licensed under Apache License v2.0 with Runtime Library Exception
//
// See http://swift.org/LICENSE.txt for license information
// See http://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
//
//===----------------------------------------------------------------------===//
//
// This file contains utility methods for parsing and performing semantic
// on modules.
//
//===----------------------------------------------------------------------===//

#include "Frontend.h"
#include "swift/AST/Identifier.h"
#include "swift/AST/ASTContext.h"
#include "swift/AST/Component.h"
#include "swift/AST/Diagnostics.h"
#include "swift/AST/Module.h"
#include "swift/AST/Stmt.h"
#include "swift/Parse/Lexer.h"
#include "swift/Subsystems.h"
#include "llvm/Support/MemoryBuffer.h"
#include "llvm/Support/PathV2.h"
#include "llvm/Support/SourceMgr.h"

using namespace swift;

static Identifier getModuleIdentifier(const llvm::MemoryBuffer *Buffer,
                                      ASTContext &Context,
                                      bool IsMainModule) {
  StringRef moduleName = Buffer->getBufferIdentifier();

  // As a special case, recognize <stdin>.
  if (moduleName == "<stdin>")
    return Context.getIdentifier("stdin");

  // Find the stem of the filename.
  moduleName = llvm::sys::path::stem(moduleName);

  // Complain about non-identifier characters in the module name.
  if (!Lexer::isIdentifier(moduleName)) {
    if (IsMainModule) {
      moduleName = "main";
    } else {
      SourceLoc Loc = SourceLoc(llvm::SMLoc::getFromPointer(Buffer->getBuffer().begin()));
      Context.Diags.diagnose(Loc, diag::bad_module_name);
      moduleName = "bad";
    }
  }

  return Context.getIdentifier(moduleName);
}

TranslationUnit*
swift::buildSingleTranslationUnit(ASTContext &Context, unsigned BufferID,
                                  bool ParseOnly, bool IsMainModule) {
  Component *Comp = new (Context.Allocate<Component>(1)) Component();
  const llvm::MemoryBuffer *Buffer = Context.SourceMgr.getMemoryBuffer(BufferID);
  Identifier ID = getModuleIdentifier(Buffer, Context, IsMainModule);
  TranslationUnit *TU = new (Context) TranslationUnit(ID, Comp, Context,
                                                      IsMainModule,
                                                      /*IsReplModule=*/false);

  unsigned BufferOffset = 0;
  unsigned CurTUElem = 0;
  do {
    parseIntoTranslationUnit(TU, BufferID, &BufferOffset);
    if (!ParseOnly) {
      performNameBinding(TU, CurTUElem);
      performTypeChecking(TU, CurTUElem);
      CurTUElem = TU->Decls.size();
    }
  } while (BufferOffset != Buffer->getBufferSize());

  return TU;
{'fraction_non_alphanumeric': 0.17943794728573922, 'fraction_numerical': 0.0048874149066154655, 'mean_word_length': 2.5239852398523985, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 17, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 13, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6685663', 'n_tokens_mistral': 1610, 'n_tokens_neox': 1529, 'n_words': 402}