Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1 | //===--- Sema.cpp - AST Builder and Semantic Analysis Implementation ------===// |
| 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
| 5 | // This file was developed by Chris Lattner and is distributed under |
| 6 | // the University of Illinois Open Source License. See LICENSE.TXT for details. |
| 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | // |
| 10 | // This file implements the actions class which performs semantic analysis and |
| 11 | // builds an AST out of a parse stream. |
| 12 | // |
| 13 | //===----------------------------------------------------------------------===// |
| 14 | |
| 15 | #include "Sema.h" |
| 16 | #include "clang/AST/ASTContext.h" |
| 17 | #include "clang/Lex/Preprocessor.h" |
| 18 | #include "clang/Basic/Diagnostic.h" |
Chris Lattner | 59907c4 | 2007-08-10 20:18:51 +0000 | [diff] [blame] | 19 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 20 | using namespace clang; |
| 21 | |
| 22 | Sema::Sema(Preprocessor &pp, ASTContext &ctxt, std::vector<Decl*> &prevInGroup) |
| 23 | : PP(pp), Context(ctxt), CurFunctionDecl(0), LastInGroupList(prevInGroup) { |
Chris Lattner | 59907c4 | 2007-08-10 20:18:51 +0000 | [diff] [blame] | 24 | |
| 25 | // Get IdentifierInfo objects for known functions for which we |
| 26 | // do extra checking. |
| 27 | IdentifierTable& IT = PP.getIdentifierTable(); |
| 28 | |
| 29 | KnownFunctionIDs[ id_printf ] = &IT.get("printf"); |
| 30 | KnownFunctionIDs[ id_fprintf ] = &IT.get("fprintf"); |
| 31 | KnownFunctionIDs[ id_sprintf ] = &IT.get("sprintf"); |
| 32 | KnownFunctionIDs[ id_snprintf ] = &IT.get("snprintf"); |
Chris Lattner | 59907c4 | 2007-08-10 20:18:51 +0000 | [diff] [blame] | 33 | KnownFunctionIDs[ id_asprintf ] = &IT.get("asprintf"); |
Ted Kremenek | e0eb80a | 2007-08-10 21:13:51 +0000 | [diff] [blame] | 34 | KnownFunctionIDs[ id_vsnprintf ] = &IT.get("vsnprintf"); |
Chris Lattner | 59907c4 | 2007-08-10 20:18:51 +0000 | [diff] [blame] | 35 | KnownFunctionIDs[ id_vasprintf ] = &IT.get("vasprintf"); |
| 36 | KnownFunctionIDs[ id_vfprintf ] = &IT.get("vfprintf"); |
| 37 | KnownFunctionIDs[ id_vsprintf ] = &IT.get("vsprintf"); |
| 38 | KnownFunctionIDs[ id_vprintf ] = &IT.get("vprintf"); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 39 | } |
| 40 | |
Chris Lattner | 394a3fd | 2007-08-31 04:53:24 +0000 | [diff] [blame] | 41 | void Sema::DeleteExpr(ExprTy *E) { |
| 42 | delete static_cast<Expr*>(E); |
| 43 | } |
| 44 | void Sema::DeleteStmt(StmtTy *S) { |
| 45 | delete static_cast<Stmt*>(S); |
| 46 | } |
| 47 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 48 | //===----------------------------------------------------------------------===// |
| 49 | // Helper functions. |
| 50 | //===----------------------------------------------------------------------===// |
| 51 | |
| 52 | bool Sema::Diag(SourceLocation Loc, unsigned DiagID) { |
| 53 | PP.getDiagnostics().Report(Loc, DiagID); |
| 54 | return true; |
| 55 | } |
| 56 | |
| 57 | bool Sema::Diag(SourceLocation Loc, unsigned DiagID, const std::string &Msg) { |
| 58 | PP.getDiagnostics().Report(Loc, DiagID, &Msg, 1); |
| 59 | return true; |
| 60 | } |
| 61 | |
| 62 | bool Sema::Diag(SourceLocation Loc, unsigned DiagID, const std::string &Msg1, |
| 63 | const std::string &Msg2) { |
| 64 | std::string MsgArr[] = { Msg1, Msg2 }; |
| 65 | PP.getDiagnostics().Report(Loc, DiagID, MsgArr, 2); |
| 66 | return true; |
| 67 | } |
| 68 | |
| 69 | bool Sema::Diag(SourceLocation Loc, unsigned DiagID, SourceRange Range) { |
| 70 | PP.getDiagnostics().Report(Loc, DiagID, 0, 0, &Range, 1); |
| 71 | return true; |
| 72 | } |
| 73 | |
| 74 | bool Sema::Diag(SourceLocation Loc, unsigned DiagID, const std::string &Msg, |
| 75 | SourceRange Range) { |
| 76 | PP.getDiagnostics().Report(Loc, DiagID, &Msg, 1, &Range, 1); |
| 77 | return true; |
| 78 | } |
| 79 | |
| 80 | bool Sema::Diag(SourceLocation Loc, unsigned DiagID, const std::string &Msg1, |
| 81 | const std::string &Msg2, SourceRange Range) { |
| 82 | std::string MsgArr[] = { Msg1, Msg2 }; |
| 83 | PP.getDiagnostics().Report(Loc, DiagID, MsgArr, 2, &Range, 1); |
| 84 | return true; |
| 85 | } |
| 86 | |
| 87 | bool Sema::Diag(SourceLocation Loc, unsigned DiagID, |
| 88 | SourceRange R1, SourceRange R2) { |
| 89 | SourceRange RangeArr[] = { R1, R2 }; |
| 90 | PP.getDiagnostics().Report(Loc, DiagID, 0, 0, RangeArr, 2); |
| 91 | return true; |
| 92 | } |
| 93 | |
| 94 | bool Sema::Diag(SourceLocation Loc, unsigned DiagID, const std::string &Msg, |
| 95 | SourceRange R1, SourceRange R2) { |
| 96 | SourceRange RangeArr[] = { R1, R2 }; |
| 97 | PP.getDiagnostics().Report(Loc, DiagID, &Msg, 1, RangeArr, 2); |
| 98 | return true; |
| 99 | } |
| 100 | |
| 101 | bool Sema::Diag(SourceLocation Range, unsigned DiagID, const std::string &Msg1, |
| 102 | const std::string &Msg2, SourceRange R1, SourceRange R2) { |
| 103 | std::string MsgArr[] = { Msg1, Msg2 }; |
| 104 | SourceRange RangeArr[] = { R1, R2 }; |
| 105 | PP.getDiagnostics().Report(Range, DiagID, MsgArr, 2, RangeArr, 2); |
| 106 | return true; |
| 107 | } |
| 108 | |
| 109 | const LangOptions &Sema::getLangOptions() const { |
| 110 | return PP.getLangOptions(); |
| 111 | } |