Chris Lattner | ddd6fc8 | 2006-11-10 04:58:55 +0000 | [diff] [blame] | 1 | //===--- Sema.cpp - AST Builder and Semantic Analysis Implementation ------===// |
Chris Lattner | 3e7bd4e | 2006-08-17 05:51:27 +0000 | [diff] [blame] | 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 | // |
Chris Lattner | ddd6fc8 | 2006-11-10 04:58:55 +0000 | [diff] [blame] | 10 | // This file implements the actions class which performs semantic analysis and |
| 11 | // builds an AST out of a parse stream. |
Chris Lattner | 3e7bd4e | 2006-08-17 05:51:27 +0000 | [diff] [blame] | 12 | // |
| 13 | //===----------------------------------------------------------------------===// |
| 14 | |
Chris Lattner | ddd6fc8 | 2006-11-10 04:58:55 +0000 | [diff] [blame] | 15 | #include "Sema.h" |
Chris Lattner | cb6a382 | 2006-11-10 06:20:45 +0000 | [diff] [blame] | 16 | #include "clang/AST/ASTContext.h" |
Chris Lattner | d3e9895 | 2006-10-06 05:22:26 +0000 | [diff] [blame] | 17 | #include "clang/Lex/Preprocessor.h" |
Chris Lattner | d6647d3 | 2007-05-16 17:56:50 +0000 | [diff] [blame] | 18 | #include "clang/Basic/Diagnostic.h" |
Chris Lattner | b87b1b3 | 2007-08-10 20:18:51 +0000 | [diff] [blame] | 19 | |
Chris Lattner | c11438c | 2006-08-18 05:17:52 +0000 | [diff] [blame] | 20 | using namespace clang; |
| 21 | |
Steve Naroff | c62adb6 | 2007-10-09 22:01:59 +0000 | [diff] [blame] | 22 | void Sema::ActOnTranslationUnitScope(SourceLocation Loc, Scope *S) { |
| 23 | TUScope = S; |
| 24 | } |
| 25 | |
Steve Naroff | 2c055d2 | 2007-02-28 19:32:13 +0000 | [diff] [blame] | 26 | Sema::Sema(Preprocessor &pp, ASTContext &ctxt, std::vector<Decl*> &prevInGroup) |
| 27 | : PP(pp), Context(ctxt), CurFunctionDecl(0), LastInGroupList(prevInGroup) { |
Chris Lattner | b87b1b3 | 2007-08-10 20:18:51 +0000 | [diff] [blame] | 28 | |
| 29 | // Get IdentifierInfo objects for known functions for which we |
| 30 | // do extra checking. |
| 31 | IdentifierTable& IT = PP.getIdentifierTable(); |
| 32 | |
| 33 | KnownFunctionIDs[ id_printf ] = &IT.get("printf"); |
| 34 | KnownFunctionIDs[ id_fprintf ] = &IT.get("fprintf"); |
| 35 | KnownFunctionIDs[ id_sprintf ] = &IT.get("sprintf"); |
| 36 | KnownFunctionIDs[ id_snprintf ] = &IT.get("snprintf"); |
Chris Lattner | b87b1b3 | 2007-08-10 20:18:51 +0000 | [diff] [blame] | 37 | KnownFunctionIDs[ id_asprintf ] = &IT.get("asprintf"); |
Ted Kremenek | cfc9419 | 2007-08-10 21:13:51 +0000 | [diff] [blame] | 38 | KnownFunctionIDs[ id_vsnprintf ] = &IT.get("vsnprintf"); |
Chris Lattner | b87b1b3 | 2007-08-10 20:18:51 +0000 | [diff] [blame] | 39 | KnownFunctionIDs[ id_vasprintf ] = &IT.get("vasprintf"); |
| 40 | KnownFunctionIDs[ id_vfprintf ] = &IT.get("vfprintf"); |
| 41 | KnownFunctionIDs[ id_vsprintf ] = &IT.get("vsprintf"); |
| 42 | KnownFunctionIDs[ id_vprintf ] = &IT.get("vprintf"); |
Steve Naroff | 38d31b4 | 2007-02-28 01:22:02 +0000 | [diff] [blame] | 43 | } |
Chris Lattner | cb6a382 | 2006-11-10 06:20:45 +0000 | [diff] [blame] | 44 | |
Chris Lattner | 57c523f | 2007-08-31 04:53:24 +0000 | [diff] [blame] | 45 | void Sema::DeleteExpr(ExprTy *E) { |
| 46 | delete static_cast<Expr*>(E); |
| 47 | } |
| 48 | void Sema::DeleteStmt(StmtTy *S) { |
| 49 | delete static_cast<Stmt*>(S); |
| 50 | } |
| 51 | |
Chris Lattner | c11438c | 2006-08-18 05:17:52 +0000 | [diff] [blame] | 52 | //===----------------------------------------------------------------------===// |
Chris Lattner | eaafe122 | 2006-11-10 05:17:58 +0000 | [diff] [blame] | 53 | // Helper functions. |
| 54 | //===----------------------------------------------------------------------===// |
| 55 | |
Chris Lattner | d6647d3 | 2007-05-16 17:56:50 +0000 | [diff] [blame] | 56 | bool Sema::Diag(SourceLocation Loc, unsigned DiagID) { |
| 57 | PP.getDiagnostics().Report(Loc, DiagID); |
| 58 | return true; |
| 59 | } |
| 60 | |
Steve Naroff | f1e5369 | 2007-03-23 22:27:02 +0000 | [diff] [blame] | 61 | bool Sema::Diag(SourceLocation Loc, unsigned DiagID, const std::string &Msg) { |
Chris Lattner | d6647d3 | 2007-05-16 17:56:50 +0000 | [diff] [blame] | 62 | PP.getDiagnostics().Report(Loc, DiagID, &Msg, 1); |
| 63 | return true; |
| 64 | } |
| 65 | |
| 66 | bool Sema::Diag(SourceLocation Loc, unsigned DiagID, const std::string &Msg1, |
| 67 | const std::string &Msg2) { |
| 68 | std::string MsgArr[] = { Msg1, Msg2 }; |
| 69 | PP.getDiagnostics().Report(Loc, DiagID, MsgArr, 2); |
Steve Naroff | f1e5369 | 2007-03-23 22:27:02 +0000 | [diff] [blame] | 70 | return true; |
Chris Lattner | eaafe122 | 2006-11-10 05:17:58 +0000 | [diff] [blame] | 71 | } |
| 72 | |
Steve Naroff | 71ce2e0 | 2007-05-18 22:53:50 +0000 | [diff] [blame] | 73 | bool Sema::Diag(SourceLocation Loc, unsigned DiagID, SourceRange Range) { |
| 74 | PP.getDiagnostics().Report(Loc, DiagID, 0, 0, &Range, 1); |
Steve Naroff | bc2f099 | 2007-03-30 20:09:34 +0000 | [diff] [blame] | 75 | return true; |
| 76 | } |
| 77 | |
Steve Naroff | 71ce2e0 | 2007-05-18 22:53:50 +0000 | [diff] [blame] | 78 | bool Sema::Diag(SourceLocation Loc, unsigned DiagID, const std::string &Msg, |
| 79 | SourceRange Range) { |
| 80 | PP.getDiagnostics().Report(Loc, DiagID, &Msg, 1, &Range, 1); |
| 81 | return true; |
| 82 | } |
| 83 | |
| 84 | bool Sema::Diag(SourceLocation Loc, unsigned DiagID, const std::string &Msg1, |
| 85 | const std::string &Msg2, SourceRange Range) { |
| 86 | std::string MsgArr[] = { Msg1, Msg2 }; |
| 87 | PP.getDiagnostics().Report(Loc, DiagID, MsgArr, 2, &Range, 1); |
| 88 | return true; |
| 89 | } |
| 90 | |
| 91 | bool Sema::Diag(SourceLocation Loc, unsigned DiagID, |
| 92 | SourceRange R1, SourceRange R2) { |
| 93 | SourceRange RangeArr[] = { R1, R2 }; |
| 94 | PP.getDiagnostics().Report(Loc, DiagID, 0, 0, RangeArr, 2); |
| 95 | return true; |
| 96 | } |
| 97 | |
| 98 | bool Sema::Diag(SourceLocation Loc, unsigned DiagID, const std::string &Msg, |
| 99 | SourceRange R1, SourceRange R2) { |
| 100 | SourceRange RangeArr[] = { R1, R2 }; |
| 101 | PP.getDiagnostics().Report(Loc, DiagID, &Msg, 1, RangeArr, 2); |
| 102 | return true; |
| 103 | } |
| 104 | |
| 105 | bool Sema::Diag(SourceLocation Range, unsigned DiagID, const std::string &Msg1, |
| 106 | const std::string &Msg2, SourceRange R1, SourceRange R2) { |
| 107 | std::string MsgArr[] = { Msg1, Msg2 }; |
| 108 | SourceRange RangeArr[] = { R1, R2 }; |
| 109 | PP.getDiagnostics().Report(Range, DiagID, MsgArr, 2, RangeArr, 2); |
Steve Naroff | f1e5369 | 2007-03-23 22:27:02 +0000 | [diff] [blame] | 110 | return true; |
Steve Naroff | 8160ea2 | 2007-03-06 01:09:46 +0000 | [diff] [blame] | 111 | } |
| 112 | |
Chris Lattner | ac18be9 | 2006-11-20 06:49:47 +0000 | [diff] [blame] | 113 | const LangOptions &Sema::getLangOptions() const { |
Steve Naroff | 38d31b4 | 2007-02-28 01:22:02 +0000 | [diff] [blame] | 114 | return PP.getLangOptions(); |
Chris Lattner | ac18be9 | 2006-11-20 06:49:47 +0000 | [diff] [blame] | 115 | } |